Difference between `npm start` & `node app.js`, when starting app?
Asked Answered
W

2

195

I have installed an application using the command express new 'filename'. I have just learned that you can start an application using:

npm start

Thus far I have used:

node app.js

to start my server. Anyone know what the difference is between the two? Thanks.

Whalen answered 30/7, 2012 at 6:36 Comment(0)
M
279

From the man page, npm start:

runs a package's "start" script, if one was provided. If no version is specified, then it starts the "active" version.

Admittedly, that description is completely unhelpful, and that's all it says. At least it's more documented than socket.io.

Anyhow, what really happens is that npm looks in your package.json file, and if you have something like

"scripts": { "start": "coffee server.coffee" }

then it will do that. If npm can't find your start script, it defaults to:

node server.js

 

Mikvah answered 30/7, 2012 at 6:57 Comment(8)
Ah I see. So it's essentially the same thing unless you choose to run more scripts at compile time.Whalen
If you normally type "node server.js", then yes.Mikvah
i created one new app, npm start is not working. for old app it is running fine. any suggestions?Ghana
For example "node app.js" won't pick up router changes, while "npm start" will, as it executes package.json start script (like "node ./bin/www")Irrespective
There is of course a cost in terms of memory used and wall-clock time on calling npm if you don't need to.Lampblack
if you start with 'npm' it can restart the app by just typing 'rs' on console.Goodhen
Your comments is also unhelpful for beginners.Jackfruit
Is there some kind of search behavior built in to npm start? If I run it in a folder with nothing related to js at all, it still tries to start an application at bin/index.jsPipestone
G
11

The documentation has been updated. My answer has substantial changes vs the accepted answer: I wanted to reflect documentation is up-to-date, and accepted answer has a few broken links.

Also, I didn't understand when the accepted answer said "it defaults to node server.js". I think the documentation clarifies the default behavior:

npm-start

Start a package

Synopsis

npm start [-- <args>]

Description

This runs an arbitrary command specified in the package's "start" property of its "scripts" object. If no "start" property is specified on the "scripts" object, it will run node server.js.

In summary, running npm start could do one of two things:

  1. npm start {command_name}: Run an arbitrary command (i.e. if such command is specified in the start property of package.json's scripts object)
  2. npm start: Else if no start property exists (or no command_name is passed): Run node server.js, (which may not be appropriate, for example the OP doesn't have server.js; the OP runs nodeapp.js )
  3. I said I would list only 2 items, but are other possibilities (i.e. error cases). For example, if there is no package.json in the directory where you run npm start, you may see an error: npm ERR! enoent ENOENT: no such file or directory, open '.\package.json'
Guttapercha answered 6/5, 2017 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.