Fix - npm start: sh: index.js: command not found

Error:

# npm start

# my-app@1.0.0 start
# index.js

sh: index.js: command not found

If you are trying to start a nodeJs application using npm command and you get the above error, then below are some of the reasons for it.


Reasons for the error:

  • You have not created the index.js file.
  • You have placed the index.js file not in the root folder of your application.
  • You have not added the start script inside package.json file.
  • You have added the wrong valve for the start command.

Solution:

Below is an example of how a simple setup for your node application should look like.


package.json
{
  "name": "my-app",
  "version": "1.0.0",
  "description": "Learning npm and node",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "code2care",
  "license": "ISC",
  "dependencies": {
  }
}

Make sure scripts have the property: "start": "node index.js"


index.js
console.log("Hello node.js")
Fix - npm start error - node

Instead of npm command, you can also make use of the node command to run your application.

node index.js

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!