【问题标题】:Unable to launch express with npm start无法使用 npm start 启动 express
【发布时间】: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


    【解决方案1】:

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

    来自NPM docs

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

    【讨论】:

    • 谢谢丹尼尔。我必须在 src/app.js 之前添加节点。
    猜你喜欢
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 2023-01-13
    • 1970-01-01
    • 2015-12-22
    • 2018-05-30
    • 1970-01-01
    相关资源
    最近更新 更多