node.js 启动应用程序时,`npm start` 和 `node app.js` 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11716421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:06:13  来源:igfitidea点击:

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

node.jsexpressnpm

提问by ReneGAED

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

我已经使用命令安装了一个应用程序express new 'filename'。我刚刚了解到您可以使用以下方法启动应用程序:

npm start

Thus far I have used:

到目前为止,我使用过:

node app.js

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

启动我的服务器。有谁知道这两者有什么区别?谢谢。

回答by Yusuf X

From the man page, npm start:

手册页,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.

诚然,这种描述完全没有帮助,仅此而已。至少它比 socket.io 有更多的文档记录。

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

无论如何,真正发生的是 npm 在您的package.json文件中查找,如果您有类似

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

“脚本”:{“开始”:“咖啡服务器。咖啡”}

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

然后它会这样做。如果 npm 找不到您的启动脚本,则默认为:

node server.js

节点服务器.js

 

 

回答by The Red Pea

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:

另外,我不明白接受的答案何时说“它默认为node server.js”。我认为文档阐明了默认行为:

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.

npm-start

开始打包

概要

npm start [-- <args>]

描述

这将运行在包的startscripts”对象的“ ”属性中指定的任意命令。如果没有start在 " scripts" 对象上指定 " " 属性,它将运行node server.js

In summary, running npm startcould do one of two things:

总之,跑步npm start可以做两件事之一:

  1. npm start {command_name}: Run an arbitrary command (i.e. if such command is specified in the startproperty of package.json's scriptsobject)
  2. npm start: Else if no startproperty exists (or no command_nameis 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.jsonin the directory where you run npm start, you may see an error: npm ERR! enoent ENOENT: no such file or directory, open '.\package.json'
  1. npm start {command_name}: 运行任意命令(即,如果在startpackage.jsonscripts对象的属性中指定了此类命令)
  2. npm start:否则,如果不start存在任何属性(或没有command_name传递):运行node server.js,(这可能不合适,例如 OP 没有server.js;OP 运行nodeapp.js
  3. 我说我只会列出 2 个项目,但还有其他可能性(即错误情况)。例如,如果package.json您运行的目录中没有npm start,您可能会看到错误:npm ERR! enoent ENOENT: no such file or directory, open '.\package.json'