How To Fix The missing script: “start” npm Error

missing script start npm error

Novice Node.js developers frequently encounter the missing script: “start” npm error. Here is why it happens and what you can do to resolve it.

The missing script: “start” npm Error

npm start

When you run ‘npm start’ in the root folder of a project to start it, you may see this error instead:

$ npm run start
npm ERR! Missing script: "start"

As the error message has pointed out, npm has failed to find the necessary command required to start the app.

According to npm’s documentation, the ‘npm start’ command first finds and runs the code defined in the ‘start’ property of the ‘scripts’ section in the package.json file.

This file is located in the root folder of every npm package and is used to store essential metadata. npm parses this file to know the modules required for your package (including their versions), its license and author, and scripts npm needs to automate various tasks.

Using the package.json file makes dependencies and metadata management easier for Node.js developers. The ‘scripts’ section typically contains several entries. Each entry is a property-value pair.

This is how they look like in a new create-react-app project:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

 There are four entries here: start, build, test, and eject. Each of them comes with a command used for running a common task when developing a Node.js app. For instance, the ‘start’ tells npm that it needs to execute ‘react-scripts start’ to start the project.

The reason we include these commands in the package.json file is to streamline the development process.

You don’t need to type the  ‘react-scripts start’ command every time you need to test-run your project. Just use ‘npm start’ instead, and npm will know what to execute under the hood to carry out that task.

But to take advantage of this shortcut, the ‘start’ property has to be defined first in the package.json file. When there is no such property, npm will run its default command ‘node server.js’. If there is also no such file, npm will produce the error:

npm ERR! missing script: start

Solution

To fix this issue, all you need to do is to add the ‘start’ property to the package.json file of your project before starting it.

What should be added varies between apps. It depends on how you write your project and how you plan to run it. Sometimes, you may only need this simple start command:

"scripts": {
   "start": "node app.js"
},

After adding the ‘script’ property, you can test it by running the ‘npm run-script’ command. It should display every valid script defined in the package.json file. Your output should have a start section in it like this:

$ npm run-script
Lifecycle scripts included in [email protected]:
  start
    react-scripts start
  test
    react-scripts test

Another option is to put the code of your app into the server.js file. With this, you don’t need to define the ‘script’ property. npm will automatically execute the server.js file whenever you run ‘npm start’.

Note: if npm produces the error Could not find module, this guide can help you fix it.

Summary

The missing script: “start” npm error occurs when you forget to add the ‘start’ property into the package.json file. Fix the problem by defining the command used to run your project in that file.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *