How To Solve Error “Cannot Find Module ‘Prettier'” In Javascript

Cannot find module 'prettier' in Javascript

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:

Leave a Reply

Your email address will not be published. Required fields are marked *