Javascript npm start vs node app.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31362021/
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
npm start vs node app.js
提问by dudewad
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 非常陌生,并试图了解应用程序基础知识。我很好奇为什么这两个命令:
node app.js
节点应用程序.js
--vs--
--vs--
npm start
启动
output the same thing to the console and appear to continue "listening", but why when I try to access http://localhost:3000I get a 404 only when running the first command.
向控制台输出相同的内容并似乎继续“侦听”,但是为什么当我尝试访问http://localhost:3000时,仅在运行第一个命令时才得到 404。
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?
我看到 Express 4 似乎具有不同的应用程序结构,但为什么尽管控制台中的行为相同,但为什么一个成功侦听另一个没有?
Any explanation is helpful. Thanks!
任何解释都是有帮助的。谢谢!
采纳答案by andyberry88
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.jsonfor more info. So if you had the following package.json then the commands are completely different.
这两个命令不一定相同。npm start
运行“start”脚本配置中的任何内容,按照“package.json”中的定义运行,node app.js
执行“node”中的“app.js”文件。有关更多信息,请参阅http://browsenpm.org/package.json。所以如果你有以下 package.json 那么命令就完全不同了。
{
"name": "my cool node project",
....
"scripts": {
"start": "node index.js"
}
....
}
The following package.json is what you'll want to make them identical.
以下 package.json 是您想要使它们相同的内容。
{
"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.
我首先检查“启动”脚本设置为运行的内容,然后尝试直接在 CLI 中运行相同的命令,而不是通过 NPM 来查看不同之处。
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.
如果服务器返回 404,则表明服务器正在侦听,但文档根目录或访问权限未正确设置,因此它返回“找不到文件”响应。
回答by BlackBeard
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.
在npm start
没有scripts
部分的情况下执行package.json
将导致在该目录中npm
查找server.js
,如果找到,则使用node server.js
else运行它,它将npm ERR! missing script: start
作为错误消息抛出。
Documentation: npm-start
文档:npm-start
回答by Pradeep
Few more things I would like to add, may help future audience
我还想补充一些东西,可能会对未来的观众有所帮助
First of all
首先
Node- is run time for any javascript code
节点- 是任何 javascript 代码的运行时
NPM is package manger, which can be used to download/update/run packages and many more, consisting of 3 things
NPM 是包管理器,可用于下载/更新/运行包等等,由 3 项组成
- Website
- npm CLI
- the registry
- 网站
- 命令行界面
- 登记处
Read hereto 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
node any.js- 这将简单地运行 javascript 文件“any,js”。所以如果那里没有启动服务器的代码,你会得到错误
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
npm start- 将运行 package.json 中的 start 命令。对于非常基本的示例,如果下面是 package.json 中的启动脚本
It will simply print "Hello" on console.
它只会在控制台上打印“Hello”。
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
如果您使用 CRA 创建 React 应用程序,您通常会在本节中有“ react-scripts start”。设置开发环境并启动服务器,以及热模块重新加载
That is the reason you donot get error in this case
这就是你在这种情况下不会出错的原因