Node.js: How to set npm start? - node.js

I tried to use npm start to start my application. So I added the following line in my package.json file:
"scripts": {
"start": "node app.js"
}
however, when I used npm start, I met with this problem:
but when I used node app.js, it worked. So what's the problem?

Use the command npm run start instead of npm start.

Related

Npm start and npm run build, why to use `run` when build? [duplicate]

I have checked both commands npm start and npm run start, both works perfectly. I used create-react-app. But to make configuration changes in the CSS module, I run npm eject but it throws an error.
But npm run eject worked? I'm confused on why npm eject didn't work. Can I configure this?
Below is my package.json:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
npm test, npm start, npm restart, and npm stop are all aliases for npm run xxx.
For all other scripts you define, you need to use the npm run xxx syntax.
See the docs at https://docs.npmjs.com/cli/run-script for more information.
npm start is the short form for npm run start. So, its one and the same thing.
One interesting thing to note is,
If the scripts object does not have a "start" property in package.json file, npm start or npm run start from command line will run node server.js by default.
But if the scripts object in package.json has "start" property, it overrides node server.js and executes the command in "start" property.
See - https://docs.npmjs.com/cli/v7/commands/npm-start#description
Telling this because sometimes this can be help to someone.
Inside the jenkins pipelines you should use npm commands with "run"
ex- npm run start
if not the command will not be executed.

How do I get SASS with auto-refresh?

When entering the command
npm install sass --watch ...
I get back,
npm ERR! enoent This is realated to npm not being able to find a file.
Though, the file is there and everything is spelled correctly.
Can anyone help?
Hey John take a look at this reply. Basically there are different solutions:
Single Ampersand solution
Adding to your package.json the following:
"dev:watch" : "npm run sass:watch & npm run livereload"
Parallelshell solution
Using Parallelshell and adding to your package.json the following:
"serve": "live-server",
"start": "parallelshell \"npm run scss && npm run scss -- -w\" \"npm run serve\""
Concurrently solution
Using Concurrently. Install it npm install concurrently --save-dev and add the script:
"dev:watch": "concurrently \" npm run sass:watch \" \" npm run livereload \" "

npm ERR! missing script: deploy

I am deploying my application to github pages but when I run command npm run deploy but i get error
npm ERR! missing script: deploy
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\whoami\AppData\Roaming\npm-cache\_logs\2020-02-05T16_46_06_946Z-debug.log
I have defined deploy in my package.json file
...
"scripts": {
"test": "jest --watchAll --verbose --coverage --runInBand",
"start": "node index.js",
"predeploy":"npm run build",
"deploy":"gh-pages -d build"
}
...
make sure you have save package.json file. In my case i have not saved it so that is why error is coming
Please check the terminal path. In my case, I forgot to change the directory inside my project.
Simply type the following:
cd *name-of-your-project*
Please close the current working terminal and open new terminal and try the command again.

`npm start` complains "missing script: start", without running node

I have the following files in my NPM package:
package.json:
{
"name": "npm-start",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
index.js
console.log('Hello world!');
When I try to run npm start, I get an error message:
> npm start
npm ERR! missing script: start
Okay, it's true: I don't have a "start" property in "scripts" object. But NPM's CLI Documentation claims this about npm start:
If no "start" property is specified on the "scripts" object, it will run node server.js.
Why is NPM failing me? Isn't it supposed to invoke node in this scenario? Am I missing something? (Of course, manually invoking node works fine).
In case it's relevant:
> npm -version
5.3.0
As soon as I finished typing the question, I realized my error.
When "start" property is absent, NPM will invoke node with server.js. But I don't have a server.js. NPM checks under the hood for this server.js file, and fails if it's not present. Ideally though, NPM should have reported that server.js wasn't found. The current way of handling things is confusing.
As soon as you have a server.js file, npm start works fine.
I had this issue as well. I re-installed my npm#4 and my node.js#6.4.0. Then created my project again using the commands
create-react-native-app AwesomeProject
sudo npm start (mac command)
It worked for me!

How to run start scripts from package.json?

I've two scripts in package.json
"start:dev": "nodemon ./src/index.js",
"start": "npm run build && node ./build/index.js",
npm start works well.
I need to run "start:dev": "nodemon ./src/index.js"
For most custom npm scripts you need to add run before script name
npm run start:dev
npm - The main scripts such as start, stop, restart, install, version or test do not require run command. These scripts and some other are described in npm documentation.
npm start
The others need run command before the script name as was pointed by David.
npm run start:dev
Yarn - You can also use yarn and in that case you do not have to specify run.
yarn start
yarn start:dev

Resources