Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm start vs node app.js

I'm extremely new to Node and trying to get my head around app basics. I'm curious as to why these two commands:

node app.js

--vs--

npm start

output the same thing to the console and appear to continue "listening", but why when I try to access http://localhost:3000 I get a 404 only when running the first command.

I see that Express 4 seems to have a different app structure, but why is it that one successfully listens and the other doesn't, despite the same behavior in the console?

Any explanation is helpful. Thanks!

like image 229
dudewad Avatar asked Jul 11 '15 21:07

dudewad


People also ask

Does npm start use node JS?

Description. This runs a predefined command specified in the "start" property of a package's "scripts" object. If the "scripts" object does not define a "start" property, npm will run node server. js .

What is npm start and node?

In short, npm is a package manager, and node is a javascript run time. npm start looks into the start script in your package.json; try look into package.json from a sample node.js project, package.json. so npm start actually run node ./bin/www.

Is npm run the same as node?

npm is the command-line interface to the npm ecosystem. It is battle-tested, surprisingly flexible, and used by hundreds of thousands of JavaScript developers every day. Node. js belongs to "Frameworks (Full Stack)" category of the tech stack, while npm can be primarily classified under "Front End Package Manager".

What does npm start actually do?

So npm start runs the node script that is listed under start in the package. json. As in the article that cbr mentioned in a comment, in the case of create-react-app , this happens: A new instance of the WebpackDevServer from the library of the same name is created, passing in the compiler and configuration.


2 Answers

The two of these commands aren't necessarily the same. npm start runs whatever the 'start' script config says to run as defined in your 'package.json', node app.js executes the 'app.js' file in 'node'. See http://browsenpm.org/package.json for more info. So if you had the following package.json then the commands are completely different.

{     "name": "my cool node project",     ....     "scripts": {         "start": "node index.js"     }     .... } 

The following package.json is what you'll want to make them identical.

{     "name": "my cool node project",     ....     "scripts": {         "start": "node app.js"     }     .... } 

I'd start by checking what the 'start' script is set to run and try running the same command directly in your CLI rather than through NPM to see where the difference is.

but why is it that one successfully listens and the other doesn't

If the server is returning a 404 this would suggest the server is listening, but either the document root or access permissions aren't being setup properly so it returns a 'File not Found' response.

like image 185
andyberry88 Avatar answered Oct 07 '22 12:10

andyberry88


In addition to above answer I'd like to add a point:

Doing npm start without having scripts portion in your package.json will result in npm looking for server.js in that directory, if found run it using node server.js else it'll throw npm ERR! missing script: start as the error message.

Documentation: npm-start

like image 39
BlackBeard Avatar answered Oct 07 '22 12:10

BlackBeard