First, sorry for my bad english.
I'm trying to deploy an app to Heroku using the command heroku local web in terminal, but I'm getting ERR! missing script: start, even though I have the script in my json file. And if I try to run npm start in terminal the same error appears. I searched for similar problems in Stack Overflow but I couldn't solve.
My .json is:
{
"name": "server",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
.
.
My dependencies here
.
.
},
"scripts": {
"start": "node index.js",
"test": "echo \"Error: nko test specified\" && exit 1"
}
}
PS.: When I run npm install in terminal, the json file just erases the script and everything I wrote, for example I added "config" : { "port" : "3000" }, but after running npm install it just disappeared.
I think you confused yarn and npm
If you want to use yarn and if you package.json contain a key like start you can call yarn start
But, if you want to work with npm you need to use npm run start the run it's important because npm can't understand.
Related
I recently created an NPM package called Project_1 (which I installed in another of my Node.js projects called Project_2) using the command:
npm install --save ./path/to/Project_1
(Is there a better method to install a local package inside one other locally?)
So the packaje.json of Project_2 is as follows:
{
"name": "Project_2",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"dependencies": {
"Project_1": "file:../Project_1",
}
}
In the npm package I created (Project_1), the JSON package is as follows:
{
"name": "Project_1",
"scripts": {
"custom-serve": "http-server ./website --port 8888 -o"
}
}
When I am in the root of Project_1 I can launch from the terminal:
npm run custom-serve
And in this way I can execute my custom command.
I need to know how to call npm run custom-serve inside Project_2 (also from the command line) after installing the Project_1 package as an npm dependency in Project_2.
So, in the root of Project_2 I would like to be able to run npm run custom-serve so that the command written in the Project_1 library is triggered.
How can I do this? How should I set the JSON packages? Is there a best practice for doing this? Do I need to add special scripts in .js?
This is because I have noticed that always when installing npm packages they provide commands that can be launched from the root of the project in which they are installed.
NB: The command custom-serve is just an example. I would like to create some custom commands inside the Project_1 and i want to be able to call them inside the Project_2 after the npm install of the Project_1 package.
I have already tried to create a script inside the Project_2 like so:
"scripts": {
"custom-command": "cd ./node_modules/Project_1 && npm run custom-serve",
}
But it doesn't works.
Firstly, to answer your question if there's a better way to install a local dependency, there is via npm link.
Assuming the name in Project 1's package file is project-1, you can link it in Project 2 as follows (obviously using the paths that correspond to your setup):
cd ~/projects/project-1
npm link
cd ~/projects/project-2
npm link project-1
Secondly, if you want your package to expose a runnable command, you should configure this in the bin section of its package.json. The path should point to an executable file, so to reuse the npm script, you could use execute as follows
custom-command.js
const {exec} = require('child_process');
exec('npm run custom-serve');
And then add the bin section to the package.json of Project 1:
"bin": {
"custom-command": "./custom-command.js",
}
If you have an index file inside your Project_1, and you had Project1 as a dependency for Project2, you can just call the file and it will run the default start command:
In Project_1 package.json file:
{
"name": "Project_1",
"scripts": {
"custom-serve": "http-server ./website --port 8888 -o",
"start": "npm run custom-serve"
}
}
In Project_2 package.json file:
{
"name": "Project_2",
"scripts": {
"custom-command": "node Project_1"
}
}
I'd like to add a command to the "start" script so when I do npm start, first thing that will run is npm install.
My package.json looks as follows:
.
.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "DEBUG=my-app node src/index.js",
"dev": "nodemon src/index.js"
},
.
.
.
I thought about adding npm install inside the start script:
"start": "npm install DEBUG=my-app node src/index.js",
But this doesn't work so I'd like to get a suggestion, wether if it's even possible..
I think you only use the && conector. like:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npm install && DEBUG=my-app node src/index.js",
"dev": "nodemon src/index.js"
}
Andy yes, my app should be deployed once with a single command.
This is a quite a heavy/slow start, installing all modules before starting the app.
That means if I change the code somewhwere in my node server, stop the process and run it again, a full install is gonna happen. I realize that you have a dev script with nodemon, but still.
Another case: If your app crashes on the live server and you need to start it up again then a full install will happen. What happens if a module has gone up a patch or a minor version. That means you will start up a project with different dependencies.
If you're doing this in the ci/cd then a pipeline is usually split up:
Install - npm ci
Build/compile - for example if you have typescript (not in your case)
Run all tests
Remove the devDependencies with npm prune
Start up the process
What you would maybe do is have a script called "pipeline" or something, and then call that.
"pipeline": npm ci && npm run build && npm test && npm prune && npm start
This script would then be called in your pipeline code.
The sass folders and files are in the correct place. What is wrong?
I have the package.json which I created, with this code in there.
{
"name": "starter",
"version": "1.0.0",
"description": "starter file",
"main": "index.js",
"scripts": {
"compile:sass": "node-sass sass/main.scss css/style.css -w"
},
"author": "Nez",
"license": "ISC",
"devDependencies": {
"node-sass": "^4.12.0"
}
}
As you can see the script is called compile sass when I try to run it it keeps giving the error
npm ERR! missing script: compile:sass
I have the sass compiler installed already as a dev dependency
I had this happen as well. The only way I could fix it was to move my files out of my dropbox folder on an external hard drive and move them to my main computers' hard drive. I think it has something to do with Dropbox, but I'm not sure.
One of your sass files has a syntax error
eg.
file 1 has
background: %pink
the %pink value is undefined you should check your files if %pink* is implemented
I had the same issue and tried everything until I simply saved the project files within my code editor and then all of a sudden it worked.
Add this to your code all you need to do is add npx before your node
I.e. npx node...
Then go to your terminal to that directory and run this command npm run compile:sass
Hope this works...because it worked for me will work for you too
I think you should check your file path
After 3 years!
for me it's solved by rewriting the package.json "scripts" line :
"compile:sass": "node-sass sass/main.scss css/style.css -w"
Go to package.json
Add below lines
"scripts": {
"sass": "sass sass/main.scss css/style.css",
"test": "echo \"Error: no test specified\" && exit 1"
},
For compilation use command:
npm run sass
Note: I have used main.scss as source file name
I need to build My React NextJS Project on local to host it at Appache server, when I run command run build it did not generate build folder.
I R & D on it, everyone recommend ZEIT – Next.js build with Now but it build project on cloud but I need a build for my local appache server. So please help me out.
Here is an my of package.json:
......
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "next start"
},
......
After so many struggle, I found answer of my question I have to add following line in my package.json under scripts:
"scripts": {
......
"export": "npm run build && next export"
.....
},
Basically I my case, npm run build && next export was complete required command to build NextJS project. So after adding this into package.json you just need to run in terminal:
npm export
and it will generate complete build for nextjs project.
You have a package.json script called build that runs next build. The next build command by default builds the app for development, which doesn't create a production bundle.
In order to create the production bundle on your local machine, you need to specify that you're on production environment through NODE_ENV=production (this is done automatically in now and other deployment servers). Basically, your package.json would end up:
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "next start",
"prod:build": "NODE_ENV=production npm run build"
},
You can replace npm run build with next build directly if you prefer that.
<=== To anyone experiencing the same issue reinstalling the computer works ===>
I've had to reinstall my computer, had to reinstall Node and NPM and don't know what's gone wrong and it's driving me crazy.
I'm on a Windows 10 64-bit computer, I run npm init and it works fine. I can install dependencies with no trouble, both locally and globally, but using npm install <node_module> --save doesn't update my package.json file with dependencies.
I've reinstalled both npm and node, no change.
I've tried running cmd as administrator, nothing.
I've restarted the computer too, still nothing.
If anyone knows how to solve this, please help. I'm desperate!
Update #1: when installing some modules I see that they create .cmd files in my project folder, if this is a clue, haven't experienced that before
Update #2:
Just created a fresh install using npm init and then tried npm install express --save and this is the package.json file:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
I fixed this by completely wiping and reinstalling my computer. If there's another fix to this I don't know it, but this is one fix... The nuclear way.
Just a suggestion, you may try to add "sudo" before your command.