如何用npm Start正确创建和启动express?当我执行“npm start”时,我会得到以下错误。在WebServer@1.0.0启动脚本上失败。
我在错误日志中看到了以下提示app.js的内容,但是app.js有什么不正确的地方?谢谢。
20 error code ELIFECYCLE
21 error errno 126
22 error webserver@1.0.0 start: `./src/app.js`
22 error Exit status 126
23 error Failed at the webserver@1.0.0 start script.
我有以下package.json文件。
{
"name": "webserver",
"preferGlobal": true,
"version": "1.0.0",
"author": "Milton Centeno <centem@gmail.com>",
"description": "a simple express web server",
"license": "MIT",
"main" : "./src/app.js",
"engines": {
"node": ">=0.10"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"express": ">=4.15.3"
}
}
下面是tree命令的输出,它显示了app.js文件相对于package.json的位置。
.
├── node_modules
├── package-lock.json
├── package.json
└── src
└── app.js
这就是我的app.js所用的
// Load the Express package as a module
const express = require("express");
// Access the exported service
const app = express();
// Return a string for requests to the root URL ("/")
app.get("/", (request, response) => {
response.send("Hello from Express!");
});
// Start listening to incoming requests
// If process.env.PORT is not defined, port number 3000 is used
const listener = app.listen(process.env.PORT || 3000, () => {
console.log(`Your app is listening on port ${listener.address().port}`);
});
您需要定义一个start
脚本:“start”:“node src/app.js”,
。
从NPM文档中:
这将运行在包的“scripts”
对象的“start”
属性中指定的任意命令。如果没有在“scripts”
对象上指定“start”
属性,则它将运行节点server.js
。