Как правильно создать и запустить экспресс с запуском npm? Когда я делаю «npm start», я получаю следующую ошибку. Failed at the [email protected] start script.
Я вижу в журнале ошибок следующее, что намекает на app.js, но что именно в app.js неверно? Спасибо.
20 error code ELIFECYCLE
21 error errno 126
22 error [email protected] start: `./src/app.js`
22 error Exit status 126
23 error Failed at the [email protected] start script.
У меня есть следующий файл package.json.
{
"name": "webserver",
"preferGlobal": true,
"version": "1.0.0",
"author": "Milton Centeno <[email protected]>",
"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"
}
}
Вот вывод команды дерева, который показывает, где мой файл 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.info(`Your app is listening on port ${listener.address().port}`);
});
Вам нужно определить скрипт start
: "start": "node src/app.js",
.
Из документов NPM:
Это запускает произвольную команду, указанную в свойстве
"start"
пакета его"scripts"
объекта. Если для объекта"start"
не указано свойство"scripts"
, он будет запущенnode server.js
.
Спасибо Даниэль. Мне пришлось добавить узел перед src/app.js.