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

Global formatter on macOS - cannot find package 'prettier-plugin-java' #630

Open
Maxeh opened this issue Jan 3, 2024 · 2 comments
Open

Comments

@Maxeh
Copy link

Maxeh commented Jan 3, 2024

Hello, I followed the readme and tried to configure the prettier plugin globally. For this I installed prettier and prettier-plugin-javaglobally with npm -g and added the file prettierrc.yaml to the root directory with the following content as described in the readme:

plugins:
  - prettier-plugin-java

When I run prettier --write "**/*.java" in the root directory I see the following error:
[error] Cannot find package 'prettier-plugin-java' imported from /Users/currentuser/projectroot/noop.js

It seems that it tries to load the plugin from the project directory and not from the global node_modules which is quite annoying. As others do not seem to have this kind of issue, this might only occur on macOS ?

When changing the plugin path in the prettierrc.yaml to an absolute path it works as expected:

plugins:
  - "/Users/currentuser/.nvm/versions/node/v18.16.1/lib/node_modules/prettier-plugin-java/dist/index.js"

However, this means that the file cannot be checked into Git and every developer has to configure the file since everyone has different paths; I cannot even replace /Users/currentuser with ~ , this also throws the same error.

Do you know how I can make it work on macOS so that I can only use the short version and do not need the absolute path? If there is no way to make it work, the documentation of this project should be adjusted and this issue must be described in the readme. It took me way too long to figure out how to get rid of the error.

@jtkiesel
Copy link
Contributor

This appears to be an issue with the way Prettier itself loads plugins: prettier/prettier#15141 A workaround is to install the plugin in your home directory instead of globally. That seems to allow Prettier to find the plugin without issue. For example:

npm -g install prettier
cd ~
npm install prettier-plugin-java

Thanks for bringing this to our attention! We should update Prettier Java's README to instruct users to follow the above steps, since apparently installing both Prettier and the plugin globally no longer works (I'm guessing this is an issue with Prettier 3.)

@frdwhite24
Copy link

frdwhite24 commented Jul 9, 2024

Thanks for the workaround, I experienced the same issue and can confirm this works for me for now 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants