Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failed to load prettier from package.json #1066

Closed
talentlessguy opened this issue Nov 17, 2019 · 37 comments
Closed

failed to load prettier from package.json #1066

talentlessguy opened this issue Nov 17, 2019 · 37 comments
Labels
bug locked Please open a new issue and fill out the template instead of commenting.

Comments

@talentlessguy
Copy link

talentlessguy commented Nov 17, 2019

Summary

I receive an error when I open package.json file from VS Code

Steps To Reproduce:

  1. Open package.json with Prettier extension enabled
  2. Add these to settings.json:
"prettier.configPath": "/home/v1rtl/.prettierrc.yml",
"editor.defaultFormatter": "esbenp.prettier-vscode",

and create ~/.prettierrc.yml:

printWidth: 80
semi: false
singleQuote: true
useTabs: false

Expected result

I would open package.json with no errors

Actual result

It dropped this error:

Failed to load module. Attempted to load prettier from package.json.

Also in logs I saw this:

[INFO - 1:36:27 PM] Failed to load prettier from .

Additional information

VS Code Version: 1.40.0

Prettier & Prettier Plugin Version: 3.6.0

OS and version: Manjaro, Linux 4.19.84

@giltayar
Copy link

Same here. It made VSCode so unstable ("Host extension" kept dying), that I had to uninstall the extension.

Note that I use a workspace with lots of projects in it, so that may be a problem.

@ahmedsaiddev50
Copy link

i had the same problem, after update vscode , just updated devDependencies. eslint, prettier, and now working

@JounQin
Copy link
Member

JounQin commented Nov 18, 2019

Can we fallback to global installed prettier bin instead if prettier not found from package.json?

@giltayar
Copy link

@JounQin - the problem isn't really that it couldn't find prettier. Prettier is definitely there. Unfortunately, the logs don't say much, except that (in my case at least), it's trying and failing to load prettier from lots of projects in my workspace.

@JounQin
Copy link
Member

JounQin commented Nov 18, 2019

@giltayar Maybe you're right there. But we sometimes will work on projects without package.json, ruby app for example, it should be supported out of box.

@giltayar
Copy link

giltayar commented Nov 18, 2019 via email

@JounQin
Copy link
Member

JounQin commented Nov 18, 2019

@giltayar You're right again, haha.

@ntotten
Copy link
Member

ntotten commented Nov 18, 2019

Could somebody provide a sample github repo i can use to duplicate this error. It seems like this is something you are seeing in large monorepos maybe.

@JounQin
Copy link
Member

JounQin commented Nov 18, 2019

@ntotten Can you try eslint-mdx?

@giltayar
Copy link

@ntotten - unfortunately, the repo this happens in is closed-source, so I can't show this. But this is the prettier extension log from there: https://gist.github.com/giltayar/0522c58f252140c79c739abd9c56cb8e

I tried it, BTW, with another monorepo of mine (open-source), but unfortunately there were no problems there.

@tunjioye
Copy link

The Failed to load module. Attempted to load prettier from package.json. error notification started after updating to VS Code to version 1.40.1.

For now, you can solve it by setting Prettier Path in your VS Code Settings.


https://stackoverflow.com/questions/58866847/in-vs-code-im-getting-this-error-failed-to-load-module-attempted-to-load-pr/58934707#58934707

1. Install Prettier Globally via npm if you have never installed it globally

npm i prettier -g

2. Search & Use the Prettier Path Extension Settings in your VS Code Settings

enter image description here

// You can navigate to VS Code Settings > Extensions > Prettier for all Prettier Extension Settings

3. Update the Prettier Path to your globally installed Prettier.

For Example

/usr/local/lib/node_modules/prettier (Mac OS)

\AppData\Roaming\npm\node_modules\prettier (Windows)

@ntotten
Copy link
Member

ntotten commented Nov 21, 2019

There seem to be a few different issues going on here.

  1. Prettier must be included in the package.json. Not just installed in the node_modules, but listed in the package.json. This is something I may change later, but that is the behavior now.

  2. In one of the logs provided I am seeing this error [ERROR - 8:10:32 AM] Outdated version of prettier installed. Falling back to bundled version of prettier. You need to make sure you are using prettier version 1.13 or above otherwise it will fallback to the bundled prettier.

  3. Global modules aren't supported yet and never have been. This is also something we might change, but its not supported. As others have noted, you can make them work by setting the path to the global module in the Prettier Path setting.

@JosNun
Copy link

JosNun commented Nov 21, 2019

Similar problem here. None of the issues mentioned in the previous issue apply. Unfortunately, this is also a closed-source application.

I will note, however, that my stylesheets are in a folder that has a package.json file that isn't for a npm package (different format).

@jedwards1211
Copy link

I'm confused, is prettier-vscode using the version of prettier I have installed in the package containing the file being formatted? Want to make sure...since project differ so much in settings, plugins should always use the version of a tool installed in a given project.

@ntotten
Copy link
Member

ntotten commented Nov 23, 2019

@JosNun In your case that is likely going to be the problem. The extension searches the tree down until we file a package.json. If that package.json contains prettier, the extension uses that, otherwise it will fall back to using the bundled version of prettier. You may want to install prettier as a devDepandancy in that package.json.

@giltayar
Copy link

@ntotten all the packages in my vscode workspace have prettier installed as a dev dependency. And yes, they are all mostly updated to latest version of prettier (3.1.x and in two cases 2.7.x).

And the log you saw was probably mine, so something weird is going on here.

@ntotten
Copy link
Member

ntotten commented Nov 23, 2019

@jedwards1211

is prettier-vscode using the version of prettier I have installed in the package containing the file being formatted?

Yes, it walks down the tree and uses the closest package.json. If that package.json has prettier, it will use that otherwise it will fall back to the bundled version.

@jedwards1211
Copy link

whew okay. It seemed that after I updated prettier in my package I had to restart VSCode to get prettier-vscode to use the new installed version.

@giltayar
Copy link

Oh, wait. I looked at the versions of eslint-plugin-prettier. My bad.

Yes, I had outdated <1.13 packages in my workspace.

Unfortunately, I upgraded them, and same problem. Here is the new log: https://gist.github.com/giltayar/36aa345c64e222c4006acc3621d3baf7

@ntotten
Copy link
Member

ntotten commented Nov 27, 2019

One thing to check is if there are encoding issues in either the .prettierrc or package.json see this issue: #1082 (comment)

I added some additional logging as well in v3.11.0 to report these kinds of errors. Let me know if anyone sees this.

@ntotten ntotten added the bug label Nov 27, 2019
@giltayar
Copy link

Nope, sorry. This is my latest (prettier extension 3.11.0) log: https://gist.github.com/giltayar/c5bb12727d5c9bb4adc0cb3b67dd54a8

But I think I know what's going on (in my repo, at least). Some packages in my monorepo are not npm install-ed, because I've never worked on them (my colleagues have), and so I never needed to. Those packages are causing the module loading problem. I have figured it out by looking at the latest logs, which have more information on where the prettier load problem has happened in the monorepo (keep them!).

But this has always been so. Why is the problem popping up now? It's an amalgamation of two problems:

  1. The errors were already there, but they were suddenly made visible in VS Code. Not sure whether this is a vscode change or a change in the prettier extension.
  2. There was a bug in VS Code that crashed the extension host (Extension Host broken microsoft/vscode#84803), and the constant errors from the prettier extension triggered this for some reason. Crashing the extension host makes vscode unusable, which made me look for solutions.

Since the microsoft/vscode#84803 issue has been fixed in the latest vscode release, I still get the error messages, but I can safely ignore them as VS Code continues to to run. For me, this is good enough. I have prettier back! :-)

@giltayar
Copy link

Nope, sorry. Still getting host termination exceptions, and only if I enable the prettier extension. Have to disable it yet again... :-(

@ntotten
Copy link
Member

ntotten commented Nov 28, 2019

@giltayar im not really sure why you are seeing the extension host crash. The “errors” in the prettier extension log are handled and not going to cause a crash. They are simply being logged.

Can you provide any details on what the exception host logs or shows before the crash. I think the errors in the prettier log are probably a false flag and not the actual cause. The only result from those errors should be that formatting doesn’t happen in those parts of the monorepo.

I know your project is closed source, but if you can provide a different repo that can duplicate that issue, that would help.

As is, I don’t have enough information to solve this problem.

@hedefalk
Copy link

hedefalk commented Dec 4, 2019

I get this same error message, not on my js/ts files but only when I save my package.json. prettier is there in a recent version. I've even tried to disable formatting on json but this still happens. These are my relevant workspace settings:

"[typescript]": {
      "editor.defaultFormatter": "esbenp.prettier-vscode",
      "editor.formatOnSave": true
    },
    "json.format.enable": false

Seems to me that the plugin has registered itself as a formatter for lots of languages and when going down from package.json it doesn't find itself?

["ERROR" - 10:53:48 AM] Failed to load prettier from ''
Cannot find module 'prettier' from '/Users/viktor.hedefalk/.vscode/extensions/orta.vscode-jest-3.0.2/package.json'
Error: Cannot find module 'prettier' from '/Users/viktor.hedefalk/.vscode/extensions/orta.vscode-jest-3.0.2/package.json'
	at Function.e.exports [as sync] (/Users/viktor.hedefalk/.vscode/extensions/esbenp.prettier-vscode-3.11.0/dist/extension.js:1:318316)
	at t.ModuleResolver.findPkg (/Users/viktor.hedefalk/.vscode/extensions/esbenp.prettier-vscode-3.11.0/dist/extension.js:1:245352)
	at t.ModuleResolver.findPkg [as findPkgMem] (/Users/viktor.hedefalk/.vscode/extensions/esbenp.prettier-vscode-3.11.0/dist/extension.js:1:245710)
	at t.ModuleResolver.requireLocalPkg (/Users/viktor.hedefalk/.vscode/extensions/esbenp.prettier-vscode-3.11.0/dist/extension.js:1:244427)
	at t.ModuleResolver.getPrettierInstance (/Users/viktor.hedefalk/.vscode/extensions/esbenp.prettier-vscode-3.11.0/dist/extension.js:1:243539)
	at t.LanguageResolver.getSupportLanguages (/Users/viktor.hedefalk/.vscode/extensions/esbenp.prettier-vscode-3.11.0/dist/extension.js:1:242247)
	at t.LanguageResolver.allEnabledLanguages (/Users/viktor.hedefalk/.vscode/extensions/esbenp.prettier-vscode-3.11.0/dist/extension.js:1:241855)
	at t.default.selectors (/Users/viktor.hedefalk/.vscode/extensions/esbenp.prettier-vscode-3.11.0/dist/extension.js:1:345106)
	at registerFormatter (/Users/viktor.hedefalk/.vscode/extensions/esbenp.prettier-vscode-3.11.0/dist/extension.js:1:344257)
	at l.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:47:207)
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:556:763
	at l.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:47:207)
	at t.ExtHostFileSystemEventService.$onFileEvent (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:557:740)
	at f._doInvokeHandler (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:646:285)
	at f._invokeHandler (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:645:999)
	at f._receiveRequest (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:644:664)
	at f._receiveOneMessage (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:643:477)
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:641:754
	at l.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:47:207)
	at _.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:187:274)
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:798:285
	at l.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:47:207)
	at _.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:187:274)
	at t.PersistentProtocol._receiveMessage (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:191:629)
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:188:824
	at l.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:47:207)
	at f.acceptChunk (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:184:737)
	at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:184:89
	at Socket.t (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:193:68)
	at Socket.emit (events.js:200:13)
	at addChunk (_stream_readable.js:294:12)
	at readableAddChunk (_stream_readable.js:275:11)
	at Socket.Readable.push (_stream_readable.js:210:10)
	at Pipe.onStreamRead (internal/stream_base_commons.js:166:17)
["INFO" - 10:53:48 AM] Enabling prettier in workspace 'orta.vscode-jest-3.0.2' for languages:
[
  "javascript",
  "mongo",
  "javascriptreact",
  "typescript",
  "typescriptreact",
  "json",
  "jsonc",
  "json5",
  "css",
  "postcss",
  "less",
  "scss",
  "graphql",
  "markdown",
  "mdx",
  "html",
  "vue",
  "yaml"
]
["INFO" - 10:53:48 AM] Enabling prettier for range supported languages
[
  "javascript",
  "javascriptreact",
  "typescript",
  "typescriptreact",
  "json",
  "graphql"
]

@dgreuel
Copy link

dgreuel commented Dec 4, 2019

I was having this problem, too. When I tried to install prettier globally, I would get this error:

["INFO" - 9:47:06 AM] Loaded module 'prettier@undefined' from '/Users/dan/.config/yarn/global/node_modules/.bin/prettier'
["ERROR" - 9:47:06 AM] Outdated version of prettier installed. Falling back to bundled version of prettier.

Seems it somehow couldn't detect the version number of the globally installed prettier...

I then realized my project is using yarn pnp, so maybe that's why it couldn't find the prettier from package.json. I did yarn unplug prettier, then created a symlink to the prettier in .pnp/unplugged in the top-level node_modules and it finally worked!

@ctwowt
Copy link

ctwowt commented Dec 7, 2019

Sometimes I just want open files and reading some repo source code rather than run npm install. This repo could using prettier in package.json but I don't want install I just reading source code to study, and I don't want create a .prettierignore file because I often open several repos that cannot for every repo create a .prettierignore, so open files and show warning info to tell me need run npm install is annoying...
By the way I really hope can directly setting ignorePattern like eslint config option in ./vscode/.setteing.json rather than create a .prettierignore file

sorry, poor English...

image

@karthiknvlr
Copy link

System Config:
OS : Ubuntu 18.04
VSCode version : version 1.40
I am also getting the same issue whenever I opened the below project in VScode.
https://github.com/santiq/bulletproof-nodejs.git
image

In package.json under devDependencies prettier is added
image

@tunjioye
Copy link

tunjioye commented Dec 11, 2019

@deadcoder0904
Copy link

deadcoder0904 commented Dec 12, 2019

This has been really hectic. Been happening for a couple of weeks & now I got frustrated 🤦‍♂️

Any way to solve this?

@tunjioye your solution didn’t work for me.

I used yarn global instead of npm --global & tried path /usr/local/bin/prettier as that is where yarn installs the binary but still didn’t work :(

@tunjioye
Copy link

tunjioye commented Dec 12, 2019

WOW.
I changed my Prettier Path to my yarn global version /usr/local/bin/prettier and it worked.
@deadcoder0904 Try running open /usr/local/bin/prettier in your terminal to verify your prettier is installed globally via yarn

@deadcoder0904
Copy link

@tunjioye I already did, it’s installed FWIW

@hedefalk
Copy link

@tunjioye Just want to point out the obvious - installing globally is very much a workaround. I'm always trying to minimize any kind of global installs on a dev machine to the bare minimum. Prettier is a big no-no to install globally for me. A plugin like this needs to be able to use the prettier version from the module at hand.

@ntotten
Copy link
Member

ntotten commented Dec 13, 2019

I am really going to need somebody to provide a sample GitHub repo that I can use to reproduce this otherwise it really isn’t clear what is happening. I think this issue has evolved into a discussion of multiple, separate issues. Please provide repro steps and a sample.

@JounQin
Copy link
Member

JounQin commented Dec 14, 2019

@ntotten I tried to install prettier globally via homebrew, it failed for complaining about outdated version of prettier, but if I switched to install prettier galobally via yarn, it worked without warning tips.

And is eslint-mdx not simple enough for reproduction?

@ntotten
Copy link
Member

ntotten commented Dec 14, 2019

@JounQin I ran eslint-mdx without any errors. So I am not able to reproduce the problem.

Also, global modules are not supported unless you are setting the prettierPath config to point to them. This extension does not yet automatically detect global modules.

@JounQin
Copy link
Member

JounQin commented Dec 14, 2019

@ntotten I mean yarn global add prettier works but brew install prettier doesn't work.

I both enabled prettierPath config correctly.

@ntotten
Copy link
Member

ntotten commented Dec 14, 2019

I am going to close this issue. It has turned into a discussion of what I think are many related issues and I cannot keep up. Please open a new issue with repro steps and a sample project along will all other valid information. Thanks!

@ntotten ntotten closed this as completed Dec 14, 2019
@prettier prettier locked as off-topic and limited conversation to collaborators Dec 14, 2019
@ntotten ntotten added the locked Please open a new issue and fill out the template instead of commenting. label Apr 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug locked Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

No branches or pull requests