无法使用 npm start 启动 express

Unable to launch express with npm start

如何使用 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}`);
});

您需要定义一个 start 脚本:"start": "node src/app.js",.

来自NPM docs

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.