101

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!

dudewad
  • 13,215
  • 6
  • 37
  • 46
  • 4
    `npm start` looks into the `start` script in your package.json. It could be different script than app.js. Try look into package.json. – hassansin Jul 11 '15 at 21:36
  • Can you post your app.js and the relevant part of your package.json? – K. P. MacGregor Jul 11 '15 at 21:38
  • [Relevant link](https://docs.npmjs.com/misc/scripts) – E_net4 Jul 11 '15 at 21:39
  • Possible duplicate of [Difference between \`npm start\` & \`node app.js\`, when starting app?](http://stackoverflow.com/questions/11716421/difference-between-npm-start-node-app-js-when-starting-app) – Ismael Jun 03 '16 at 19:06

3 Answers3

95

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.

andyberry88
  • 2,244
  • 20
  • 17
  • 3
    Know what, you're right. I finally figured it out, I was a little disoriented because I'm coming from the FE automation world of using grunt too much. Basically, the ./bin/www file is now the bootstrapper for the server which in older versions of express was not the case. Thank you for helping me understand. Basically I understand that app.js doesn't fully bootstrap the application but it WAS doing things like mongodb connections, which is why I was seeing my console logs. The new express norm is using ./bin/www to perform all bootstrapping (which I like; it seems cleaner). +1 thank you!! – dudewad Jul 11 '15 at 21:43
  • @dudewad Is the "start script" in any form different from the "main" field. Because according to the npm docs, they both seem to achieve the same thing... – Nuwan Jayawardene Sep 24 '18 at 03:14
  • I am unsure what the main field does. What I've come to learn is that the "scripts" section of your package.json is what is searched when you type 'npm run [somescript]'. I think start in this case is an alias. – dudewad Sep 26 '18 at 01:08
34

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

Quintin Willison
  • 610
  • 6
  • 13
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
13

Few more things I would like to add, may help future audience

First of all

Node - is run time for any javascript code

NPM is package manger, which can be used to download/update/run packages and many more, consisting of 3 things

  1. Website
  2. npm CLI
  3. the registry

Read here to see what everything it does for you.

node any.js - this will simply run the javascript file "any,js". So if there is no code in there to start a server, you will get error

npm start - will run the start command in the package.json. For very basic example if below is the start script in your package.json

enter image description here

It will simply print "Hello" on console.

if you create react app using CRA, you will usually have "react-scripts start" in this section. Which sets up the development environment and starts a server, as well as hot module reloading

That is the reason you donot get error in this case

Pradeep
  • 615
  • 8
  • 14
  • You won't get an error if there is no code in `any.js` to start a server. Node is a Javascript interpreter, so as long as whatever JS you put in that script is valid you won't get an error. On the contrary you'll get whatever output it produces. In the case of `any.js`, if it's empty you'll get no output. That is a very important difference. I feel like you are conflating Javascript frameworks/libraries with Node.js which are two totally separate things. Node RUNS javascript code, React is a library that DOES STUFF IN JAVASCRIPT. For people looking for info this is an important distinction. – dudewad Aug 07 '19 at 19:47