简体   繁体   English

npm 开始不拾取更新的 package.json

[英]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.我正在从头开始编写一个节点应用程序,使用以下package.json ,这是非常样板。

{
    "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:我创建server.js看起来像这样:

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.当我使用npm start应用程序时,它运行良好。

Then, I created a new file called server_time.js :然后,我创建了一个名为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 :我在package.json中更改了以下行:

"start": "node server_time.js",

However, Node still seems to pick up server.js instead.然而,Node 似乎仍然选择了server.js 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 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.我什至删除了 package.json并重新定义了它,但是当我调用npm start start start 的值仍然过时。

Here's an output from my shell:这是来自我的 shell 的 output:

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 .在有人问之前,node 版本是v10.16.3 ,npm 版本是6.9.0

The reason why npm start currently works for you is because npm will default some script values based on package contents. npm start当前对您有效的原因是因为npm将根据 package 内容默认一些脚本值。

If there is a server.js file in the root of your package, then npm will default the start command to node server.js .如果 package 的根目录中有 server.js 文件,则 npm 将默认启动命令为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.因此,您在package.json中的start字段实际上并没有做任何事情,因为它不在scripts下,这是它应该属于的地方。

The correct approach is to update your package.json so that it looks like the following:正确的方法是更新您的package.json使其如下所示:

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

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 .然后npm run start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM