Skip to content

How to Resolve "Error: Cannot find module 'prettier'" Error in JavaScript

Fix the "Cannot find module prettier" error by installing Prettier as a dev dependency or performing a clean dependency reinstall to resolve ESLint integration issues.

The Error: Cannot find module 'prettier' is a common issue in modern JavaScript development, typically appearing when you try to run a linting or formatting command. This error is a direct message from Node.js: a tool in your project, most often ESLint, is configured to use Prettier, but the prettier package itself is not installed in your project's dependencies.

This guide will explain the root cause of this error and walk you through the definitive solutions. You will learn how to correctly install Prettier as a development dependency and how to perform a clean reinstall to fix any stubborn dependency-related issues.

Understanding the Root Cause: A Missing Peer Dependency

This error almost always occurs in projects that use ESLint with plugins like eslint-plugin-prettier or eslint-config-prettier. These plugins act as a bridge, allowing ESLint to run Prettier and report formatting issues as linting errors.

However, these plugins list prettier as a peer dependency. This means they expect you to have prettier installed in your project, but they don't install it for you. When ESLint runs the plugin and the plugin tries to require('prettier'), Node.js cannot find the module in your node_modules folder, and the process crashes.

Solution 1 (Most Common): Install Prettier as a Dev Dependency

The most direct solution is to install the prettier package into your project. Since Prettier is a tool used for development and not a library that your application runs in production, it should always be installed as a development dependency.

For exampl,e your ESLint configuration requires Prettier, but it's not listed in your package.json.

// .eslintrc.js
module.exports = {
  extends: ['plugin:prettier/recommended'], // This requires 'prettier'
};

The solution is to open your terminal in the root directory of your project (the same folder that contains your package.json file) and run the installation command.

For NPM:

npm install --save-dev prettier

For Yarn:

yarn add --dev prettier

Solution 2 (for Stubborn Issues): A Clean Reinstall of Dependencies

If you have already installed prettier but the error persists, your node_modules folder or package lock file might be in a corrupted or inconsistent state. A full, clean reinstall will resolve this.

Run the following commands from your project's root directory.

For macOS / Linux:

rm -rf node_modules
rm -f package-lock.json yarn.lock

npm cache clean --force

npm install

For Windows (CMD):

rd /s /q "node_modules"
del package-lock.json
del yarn.lock

npm cache clean --force

npm install

How to Verify the Installation

After running the installation, you can confirm everything is correct in two ways:

  1. Check package.json: Your package.json file should now have an entry for prettier in the devDependencies section.
    {
      "devDependencies": {
        "eslint-plugin-prettier": "^4.0.0",
        "prettier": "^2.8.0"
      }
    }
  2. Check node_modules: Look inside your node_modules folder. You should see a directory named prettier.

Practical Example: A Typical ESLint and Prettier Setup

This is a common scenario that leads to the error.

.eslintrc.js

module.exports = {
  plugins: ['prettier'],
  rules: {
    'prettier/prettier': 'error',
  },
};

package.json (before the fix)

{
  "devDependencies": {
    "eslint": "^8.0.0",
    "eslint-plugin-prettier": "^4.0.0"
    // 'prettier' is missing!
  }
}

Conclusion

The Cannot find module 'prettier' error is a straightforward dependency issue that is easy to fix once you understand the cause.

To fix it:

  • The primary solution is to install prettier as a development dependency: npm install --save-dev prettier.
  • This is necessary because ESLint plugins for Prettier list it as a peer dependency, expecting you to install it yourself.
  • If the error persists after installation, a clean reinstall of your node_modules folder will resolve any corruption or inconsistency issues.
Navigation