martinbshp
martinbshp

Reputation: 1163

Unable to launch express with npm start

How do I correctly create and launch express with npm start? When I do "npm start" I get the following error. Failed at the [email protected] start script. I see the following in the error log that hints to the app.js but what is it about the app.js that is incorrect? Thank you.

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.

I have the following package.json file.

{
    "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"
    }
}

Here is the output of the tree command that shows where my app.js file is relative to package.json.

.
├── node_modules
├── package-lock.json
├── package.json
└── src
    └── app.js

And this is what I have for my 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}`);
});

Upvotes: 0

Views: 947

Answers (1)

Dan Knights
Dan Knights

Reputation: 8368

You need to define a start script: "start": "node src/app.js",.

From the 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.

Upvotes: 3

Related Questions