【发布时间】:2021-03-20 23:27:54
【问题描述】:
如何使用 npm start 正确创建和启动 express?当我执行“npm start”时,出现以下错误。 Failed at the webserver@1.0.0 start script. 我在错误日志中看到以下提示 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}`);
});
【问题讨论】:
标签: node.js express package.json