Last modified: Jun 26, 2025 By Alexander Williams
Fix Error: Cannot find module 'prettier'
If you see the error Cannot find module 'prettier', don't worry. This is a common issue in Node.js projects. It means Prettier is not installed or not accessible.
Why does this error occur?
The error happens when Node.js can't locate the Prettier module. This usually occurs for one of these reasons:
1. Prettier is not installed in your project.
2. The module is installed in the wrong location.
3. There are permission issues.
4. The node_modules folder is corrupted.
How to fix the error
Here are the best solutions to resolve this issue.
1. Install Prettier locally
The most common fix is to install Prettier in your project. Run this command in your project root:
npm install prettier --save-dev
This installs Prettier as a dev dependency. The --save-dev
flag adds it to your package.json.
2. Check your node_modules
If Prettier is already installed, verify it exists in node_modules:
ls node_modules | grep prettier
If nothing appears, reinstall your dependencies:
rm -rf node_modules package-lock.json
npm install
3. Install Prettier globally
For CLI usage, you might need a global installation:
npm install -g prettier
But remember, global installs can cause version conflicts. Local installs are preferred.
4. Verify your require statement
Ensure your code imports Prettier correctly:
const prettier = require("prettier"); // Correct
A typo in the module name will trigger the error.
Common scenarios
Here are some specific cases where this error appears.
When using ESLint
If you're using ESLint with Prettier, ensure both are installed. Similar errors occur with ESLint or babel-eslint.
In CI/CD pipelines
Build servers often need fresh dependency installation. Always run npm install
in your pipeline.
With VS Code extensions
The Prettier VS Code extension needs the module installed. Check your workspace settings.
Prevent future issues
Follow these best practices to avoid module errors:
1. Commit package-lock.json to version control.
2. Specify exact Prettier versions in package.json.
3. Use nvm to manage Node.js versions.
4. Clean install when switching branches.
Conclusion
The Cannot find module 'prettier' error has simple solutions. Most often, installing Prettier locally fixes it. For similar module errors, check our guides on nodemon or other missing modules.
Always verify your dependencies and installation paths. Happy coding!