How to solve - NPM err missing script lint

Guide to solve - npm err missing script lint

Feb 26, 2023 | Read time 9 minutes

🔔 Table of contents

Introduction

One error that I recently came across when trying add linting to my web app project is:

NPM err missing script lint

I found this error also happening with Firebase function apps when you try to do a firebase deploy.

(Keep reading for the solution at the bottom if this is your case!)

Linting is a great way to keep your JavaScript code clean and make sure to stop bugs like variable scoping from creeping into your application.

The more detailed error output can look something like this:

npm ERR! missing script: lint

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

npm ERR! C:\Users\johnyweekend\AppData\Roaming\npm-cache\_logs\2018-04-13T01_27_59_009Z-debug.log

What does this error mean?

This error just means that you are trying to run the script lint in your package.json file, but it does not exist.

The package.json file is a configuration file used by npm (Node Package Manager) to manage a Node.js project. It is located in the root directory of the project and contains information about the project such as its name, version, dependencies, scripts, author, and more. Here are some key components of the package.json file:

The scripts attribute lists the project’s scripts. So these are command that can be triggered with the npm run command!

To fix this error we can do the following:

  1. Check your package.json file to make sure you have the lint command in your scripts section.
  2. Verify that you have installed eslint correctly
  3. Upgrade NPM and clean cache npm cache clean --force

1. Check your package.json file to make sure you have the lint command in your scripts section.

As an example, lets say we have the following package.json file for our Node application.

{
  "name": "your-package",
  "description": "…",
  "version": "1.0.0",
  
  "scripts": {
    "test": "run lint && npm run test:run",
    "test:run": "jest"
  }
}

From the above, we can see that we only have scripts for test and test:run. Theres nothing that mentions lint.

To fix this, we can add the lint command like so:

{
  "name": "your-package",
  "description": "…",
  "version": "1.0.0",
  
  "scripts": {
    "lint": "eslint . --ext .js,.ts",
    "test": "run lint && npm run test:run",
    "test:run": "jest"
  }
}

We can be a bit more fancy and add another command that runs on top of the lint command:

"lint:fix": "npm run lint -- --fix"

2. Verify that you have installed eslint correctly

If you already have the lint command (previous step - 1) in your packages.json file, then it could be an issue with eslint.

What is eslint anyway?

When we want to lint our code - a common choice is using a static code analysis tool like eslint.

ESLint checks JavaScript (or Typescript) code for potential errors, coding style issues, and other types of problems and generally help you improve your codebase quality!

Using ESLint, we can find issues such as:

  • unused variables,
  • unreachable code,
  • missing semicolons,
  • incorrectly scoped variables, etc.

To use ESLint, you must have Node.js (^12.22.0, ^14.17.0, or >=16.0.0) installed and built with SSL support.

Firstly, we need to check if eslint is installed in your project:

  1. Open up the terminal
  2. Go to your project directory and run npm list, this will give something like the below:
+-- @babel/core@7.17.10
+-- aws-sdk@2.1145.0
+-- babel-loader@8.2.5
+-- compression-webpack-plugin@10.0.0
+-- copy-webpack-plugin@11.0.0
+-- cors@2.8.5
+-- css-loader@6.7.1
+-- dom-to-image-more@2.12.0
+-- express-handlebars@6.0.6
+-- express@4.18.1
+-- firebase-functions@3.23.0
+-- firebase@9.8.3
+-- helmet@5.1.0
+-- html-loader@3.1.0
+-- html-webpack-plugin@5.5.0
+-- imports-loader@4.0.1
+-- mini-css-extract-plugin@2.6.1
+-- npm-run-all@4.1.5
+-- pg@8.7.3
+-- sass-loader@12.6.0
+-- sass@1.51.0
+-- style-loader@3.3.1
+-- webpack-cli@4.9.2
`-- webpack@5.72.1

If eslint is not there, we can do the following steps to install eslint:

  1. Make sure you are in the root folder of your project
  2. Run the command npm init @eslint/config

This will create a .eslintrc.{js,yml,json} file with the configuration for the rules that looks like this:

{
    "rules": {
        "semi": ["error", "always"],
        "quotes": ["error", "double"]
    }
}

Cleaning existing eslint packages

Now it could be that you already have eslint installed and its still giving the error (and you can see eslint in your package.json file).

In this case we can do the below steps:

  1. Remove the node_modules folder
  2. Remove the package-lock.json
  3. Run the command npm install
  4. Update your package.json with the lint command:
{
  "name": "your-package",
  
  "scripts": {
    "lint": "eslint . --ext .js,.ts"
  }
}

3. Upgrade NPM and clean cache npm cache clean --force

One fix is to try:

npm cache clean --force
npm i --force

Sometimes, your depencies are correct, but you still get the conflicting peer dependency error. This could be due to NPM still looking through the cached modules.

NPM usually stores modules in your local project folder and also a “cached” folder. This will be ~/.npm on linux/ OSX systems, or %AppData%/npm-cache for windows systems.

So the next time you install a similar module it will go off to the cache instead of downloading it! The above command just clears the cache folder.

Firebase npm --prefix "$resource_dir" run lint - error missing script: “lint”

This error of NPM err missing script lint can come up with Firebase function projects and looks like the following after running firebase deploy:

PS D:\workspace\firebase-functions> firebase deploy

    === Deploying to 'newagent-5221d'...

i  deploying functions
Running command: npm --prefix $RESOURCE_DIR run lint
npm ERR! path D:\workspace\firebase-functions\$RESOURCE_DIR\package.json
npm ERR! code ENOENT
npm ERR! errno -4058
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open 'D:\workspace\firebase-functions\$RESOURCE_DIR\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\dtlut\AppData\Roaming\npm-cache\_logs\2018-01-19T15_57_22_990Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code4294963238

So the reason you are getting this error is mainly due to when you first run firebase init, going through all the prompts, you select NO for “Do you want to install dependencies with NPM now”

firebase lint error

So when selecting NO, this would prevent the eslint package from being installed and resulting in the error!

Another cause could be that you may have deleted the eslint installed package under node_modules.

To fix this we can run npm install from the functions folder to recreate everything needed under node_modules.

Other things to try if the above did not work

So if for some reason the above solution did not work theres a few other steps for you to try to get rid of this error:

  1. Platform issue - Locate the firebase.json file in your directory and update the lint command according to your system:

Linux

"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]

Windows

"predeploy": [
"npm --prefix %RESOURCE_DIR% run lint"
]

(Note for windows we are using the % symbol)

  1. Upgrade to latest version of firebase-tools

Summary

In this article, we gave a step by step guide on fixing the NPM err missing script lint error. This error occurs because the package.json file does not have a script command of lint.

To fix this we need to add a lint command such as: "lint": "eslint . --ext .js,.ts" in our package.json.

If we already have the lint command, and this error still appears, then it could be that eslint is not installed. We can install it with the command npm init @eslint/config.

Finally this is a common problem with Firebase function applications.

👋 About the Author

G'day! I am Huy a software engineer based in Australia. I have been creating design-centered software for the last 10 years both professionally and as a passion.

My aim to share what I have learnt with you! (and to help me remember 😅)

Follow along on Twitter , GitHub and YouTube