GothamCityRises
GothamCityRises

Reputation: 2102

npm start not picking up updated package.json

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

> [email protected] start /Users/g/Repos/MyFirstNodeProject
> node server.js

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

Upvotes: 2

Views: 1668

Answers (2)

goto
goto

Reputation: 4425

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"
  }
}

Upvotes: 3

SomoKRoceS
SomoKRoceS

Reputation: 3043

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.

Upvotes: 1

Related Questions