2

I am writing a node app from scratch, with the following package.json, which is pretty boiler-plate.

{
    "name": "myfirstnodeproject",
    "version": "1.0.1",
    "description": "Learning node",
    "main": "index.js",
    "start": "node server.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "license": "ISC",
    "dependencies": {
        "request": "^2.88.2"
    }
}

I created server.js to look like this:

var http = require("http");

http.createServer((inRequest, inResponse) => {
    inResponse.end("Your IP Address is " + inRequest.connection.remoteAddress);
}).listen(9000);

When I started the app using npm start, it worked fine.

Then, I created a new file called server_time.js:

require("http").createServer((inRequest, inResponse) => {
    const requestModule = require("request");
    requestModule(
        "http://worldtimeapi.org/api/timezone/America/New_York",
        function (inErr, inResp, inBody) {
            inResponse.end(
               `Hello from my first Node Web server: ${inBody}`
            );
        }
    );
}).listen(9000);

I changed the following line in my package.json:

"start": "node server_time.js",

However, Node still seems to pick up server.js instead. I tried npm cache verify, npm cache clear --force, rm -rf node_modules, rm package-lock.json, and npm install again, but the problem didn't seem to go away. I even removed package.json and redefined it, but the value of start when I call npm start is still stale.

Here's an output from my shell:

GsMacbookPro:MyFirstNodeProject g$ cat package.json
{
  "name": "myfirstnodeproject",
  "version": "1.0.1",
  "description": "Learning node",
  "main": "index.js",
  "start": "node server_time.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "G",
  "license": "ISC",
  "dependencies": {
    "request": "^2.88.2"
  }
}
GsMacbookPro:MyFirstNodeProject g$ ls
node_modules        package-lock.json   package.json        server.js       server_time.js
GsMacbookPro:MyFirstNodeProject g$ npm start

> myfirstnodeproject@1.0.1 start /Users/g/Repos/MyFirstNodeProject
> node server.js

Before someone asks, node version is v10.16.3, and npm version is 6.9.0.

GothamCityRises
  • 2,072
  • 2
  • 27
  • 43
  • 3
    By the way, `start` should go under `scripts`, similar to what you have for `test` – goto Jun 12 '20 at 23:12
  • 2
    @goto1 is correct. The default start script is node server.js ... that’s why it keeps running that – bryan60 Jun 12 '20 at 23:15

2 Answers2

3

The reason why npm start currently works for you is because npm will default some script values based on package contents.

If there is a server.js file in the root of your package, then npm will default the start command to node server.js.

See here:

So your start field inside package.json wasn't actually doing anything because it was not under scripts, which is where it should belong.

The correct approach is to update your package.json so that it looks like the following:

{
  "name": "myfirstnodeproject",
  "version": "1.0.1",
  "description": "Learning node",
  "main": "index.js",
  "scripts": {
    "start": "node server_time.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "G",
  "license": "ISC",
  "dependencies": {
    "request": "^2.88.2"
  }
}
goto
  • 4,336
  • 15
  • 20
1

Try:

{
    "name": "myfirstnodeproject",
    "version": "1.0.1",
    "description": "Learning node",
    "main": "server_time.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node server_time.js"
    },
    "license": "ISC",
    "dependencies": {
        "request": "^2.88.2"
    }
}

and then npm run start.

SomoKRoceS
  • 2,934
  • 2
  • 19
  • 30
  • 2
    As per https://stackoverflow.com/a/51358329/2474507, `npm start` is merely an alias for `npm run start`. But I agree, keeping `start` at the root level was faulty. – GothamCityRises Jun 12 '20 at 23:16