Fixed Error - npm ERR! missing script: dev


In this article, we will learn how to fix the npm missing script dev error while running a Javascript application on our computer.

Sometimes when we try to run the development server for a JavaScript project we come across npm errors like this:

npm missing script dev or npm missing script serve

Recently while working on a node project I got the missing dev script error on my terminal like this.

fix the npm missing script dev error

Why do we get this npm error?

Well, there are a few common reasons which cause this error :

  1. Missing dev script in your package.json file
  2. Have a different name assigned to the command in the package.json file.
  3. Running the npm run dev command from a directory that doesn't have the package.json file.
  4. Missing package.json file from your project.

So how do we solve the missing script: dev error?

Fixes for this error are very simple, just follow the solutions below.

Solution 1: Add the dev script in the package.json file

Open your package.json file in your project and add the dev command in the scripts section of the file.

After that, run the command (npm run dev) from the root directory of the project.

Example:

{
  "name": "myApp,
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "node index.js", // Add in the scripts field
  },
  "dependencies": {
    "core-js": "^3.9.1",
  },
}

For most users, this fixes the issue.

Solution 2: Check if the command name "dev" is the same in package.json

If a different name is assigned in the package.json file, then renaming it to dev will solve the error.

Example :

{
  "name": "myApp,
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "serve": "node index.js", // different name (serve)
  },
  "dependencies": {
    "core-js": "^3.9.1",
  },
}

Here the command name is serve, so running npm run dev will throw an error in the terminal.

Just renaming serve to dev will solve the issue.

Solution 3: Run the command from the root directory of the project

Make sure you are running the command from the root directory where the package.json is present.

Running the command from a different folder / directory can throw missing script: dev error in the terminal.

Open your IDE or code editor in the root folder where the package.json file is located and then run the command npm run dev .

Solution 4: Initialized package.json file in your project folder

If the package.json file is missing in your project, then you have to create one in the root folder of the project using the command:

npm init -y

The npm init command creates a package.json file for our project's frontend. This file contains the information about the different npm packages we use in our project.

Once the package.json file is created, you can just add the dev command in the scripts section of the file.

So, these are the four solutions that can fix the missing script: dev error from your JavaScript project.


💾 Recommended Articles