This tutorial can help you learn how to solve the error “Cannot find module ‘prettier'” in Javascript. Follow this article to learn more about it with the explanation and examples below.
How does the error “Cannot find module ‘prettier'” in Javascript happen?
The error “Cannot find module ‘prettier'” in Javascript occurs when you do not install the ‘prettier’ module in JavaScript. Besides that, you can encounter this error because you forget to import this package in your code.
Look at the example below to learn more about this error.
import {formatWithCursor} from 'prettier'
formatWithCursor("1", { cursorOffset: 2, parser: "babel" } )
Output
Cannot find module 'prettier'
How to solve this error?
There are some solutions that can help you fix the error “Cannot find module ‘prettier'” in Javascript. To solve the problem, you have to install the ‘prettier’ module in Javascript. You can install it by running the statement in the terminal at your project.
Install the ‘prettier’ package with npm
You can fix this error by installing the ‘prettier’ module with npm.
First, you must install the ‘prettier’ package locally by running this command.
npm install --save-dev --save-exact prettier
Then, you must create an empty config file to help the editors and other tools know Prettier is being used by running this command.
echo {}> .prettier.json
Install the ‘prettier’ module with yarn
You can fix this error by installing the ‘prettier’ module with yarn.
First, you must install the ‘prettier’ package locally by running this command.
yarn add --dev --exact prettier
Then, you must create an empty config file to help the editors and other tools know Prettier is being used by running this command.
echo {}> .prettier.json
Make sure the ‘prettier’ module is contained in the json file
If the error is still not solved, open your JSON file and ensure the ‘prettier’ module is contained in the ‘devDependencies’ object.
Look at the example below.
{
// ...
"devDependencies": {
"prettier": "^2.6.1",
}
}
Install the latest ‘prettier’ module version
You can install the latest ‘prettier’ module version by running this command.
npm install --save-dev prettier@latest
Try your code
After fixing this error, you can try to run the code in the first title.
Look at the example below.
import {formatWithCursor} from 'prettier'
formatWithCursor("1", { cursorOffset: 2, parser: "babel" } )
console.log(formatWithCursor("1", { cursorOffset: 2, parser: "babel" } ))
Output
{ formatted: '1;\n', cursorOffset: 1, comments: [] }
Summary
To fix the error “Cannot find module ‘prettier'” in Javascript, you have to install the ‘prettier’ module by running the statement in the terminal in your project with npm or yarn. But you should install it by npm with this command: npm install –save-dev –save-exact prettier. It will work for the usual case. We hope this tutorial is helpful to you. Thanks!
Maybe you are interested:
- Cannot read property ‘style’ of Undefined in JavaScript
- Cannot read property ‘includes’ of Undefined in JavaScript
- Cannot read Properties of Undefined in JavaScript

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R