Невозможно запустить express с npm start

#node.js #express #package.json

#node.js #экспресс #package.json

Вопрос:

Как мне правильно создать и запустить express с помощью npm start? Когда я делаю «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" amp;amp; exit 1"
    },
    "dependencies": {
        "express": ">=4.15.3"
    }
}
 

Вот вывод команды дерева, которая показывает, где мой 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}`);
});
 

Ответ №1:

Вам необходимо определить start сценарий : "start": "node src/app.js", .

Из документации NPM:

При этом выполняется произвольная команда, указанная в свойстве пакета "start" его "scripts" объекта. Если "start" для объекта не указано свойство "scripts" , он будет запущен node server.js .

Комментарии:

1. Спасибо, Дэниел. Мне пришлось добавить узел перед src/app.js .