How to resolve npm ERR! missing script: watch issue

Posted on Jun 23, 2022

Learn how you can resolve npm ERR missing script watch issue. With code examples

Photo from Unsplash

The missing script: “watch” error means that npm can’t find the watch property in your package.json file.

The error usually appears when you run the npm run watch command from the command line.

The error message looks like this:

$ npm run watch
npm ERR! Missing script: "watch"
npm ERR!
npm ERR! To see a list of scripts, run:
npm ERR!   npm run

npm ERR! A complete log of this run can be found in:
...

The npm run watch command is used to run the watch script in your package.json property.

To resolve the missing script: “watch” error, you must have a watch script available in the scripts section of your package.json file.

Here’s an example of a watch script:

{
  "name": "n-app",
  "version": "1.0.0",
  "scripts": {
    "dev": "mix",
    "watch": "mix watch",
    "production": "mix --production"
  }
}

The content of the watch script depends on the configuration of your application.

It should look similar to this:

{
  "scripts": {
    "watch": "commands go here"
  }
}

What command you write in the script is up to you.

Once you have a watch npm script added to your package.json file, the error should be resolved.

If that doesn’t work, make sure that your package.json file doesn’t contain multiple scripts object.

Consider the following example:

{
  "name": "n-app",
  "version": "1.0.0",
  "scripts": {
    "watch": "mix watch",
  },
  "scripts": {
    "dev": "mix"
  }
}

When you have two scripts as in the example above, the second scripts section will override the first one.

You can verify the available scripts by running the npm run command as shown below:

$ npm run
Scripts available in n-app@1.0.0 via `npm run-script`:
  dev
    mix

To fix this, you need to merge the two scripts as one.

Move the dev script into the first scripts section as shown below:

{
  "name": "n-app",
  "version": "1.0.0",
  "scripts": {
    "watch": "mix watch",
    "dev": "mix"
  }
}

Save the changes, then run the npm run command again to verify the fix:

Scripts available in n-app@1.0.0 via `npm run-script`:
  watch
    mix watch
  dev
    mix

As you can see, now both watch and dev scripts are available to run from the command line.

Now you should be able to run the npm run watch command successfully. Good work! 😉

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.