prettier-plugin-tailwindcss: Prettier 3 and this plugin do not work as intended with Yarn 2 PNP
What version of prettier-plugin-tailwindcss are you using?
For example: v0.5.9
What version of Tailwind CSS are you using?
For example: v3.3.0
What version of Node.js are you using?
For example: v18.18
What package manager are you using?
For example: Yarn 2 with PNP
What operating system are you using?
For example: macOS arm
Reproduction URL
https://github.com/liamlows/Prettier-Issues
NOTE: uses yarn 2 with PNP and zero installs, all you should need to do is ensure corepack enable is run and run yarn to install unplugged dependencies.
Describe your issue I’ve been battling this issue for a few weeks now and just got around to finally making a reproduction repo for it.
When attempting to load plugins via prettier version 3, they dont seem to work as intended. I use two plugins one being a import sorting plugin and the other being this plugin. If i try and provide the plugin as stated in the installation instructions for prettier v3, eslint/prettier will fail to load the plugin saying it cannot be found. This is the error ESLint spits out (and prettier when you run a force format):
[Info - 12:42:11 PM] ESLint server is starting.
[Info - 12:42:12 PM] ESLint server running in node v18.15.0
[Info - 12:42:12 PM] ESLint server is running.
[Info - 12:42:13 PM] ESLint library loaded from: [REPO_PATH]/.yarn/sdks/eslint/lib/api.js
[Error - 12:42:14 PM] An unexpected error occurred:
[Error - 12:42:14 PM] Error: Cannot find package 'prettier-plugin-tailwindcss' imported from [REPO_PATH]/noop.js
at new NodeError (file://[REPO_PATH]/.yarn/unplugged/prettier-npm-3.1.1-072c31ec21/node_modules/prettier/index.mjs:21374:5)
at packageResolve (file://[REPO_PATH]/.yarn/unplugged/prettier-npm-3.1.1-072c31ec21/node_modules/prettier/index.mjs:22354:9)
at moduleResolve (file://[REPO_PATH]/.yarn/unplugged/prettier-npm-3.1.1-072c31ec21/node_modules/prettier/index.mjs:22386:20)
at defaultResolve (file://[REPO_PATH]/.yarn/unplugged/prettier-npm-3.1.1-072c31ec21/node_modules/prettier/index.mjs:22473:16)
at resolve (file://[REPO_PATH]/.yarn/unplugged/prettier-npm-3.1.1-072c31ec21/node_modules/prettier/index.mjs:22490:12)
at importFromFile (file://[REPO_PATH]/.yarn/unplugged/prettier-npm-3.1.1-072c31ec21/node_modules/prettier/index.mjs:22505:16)
at importFromDirectory (file://[REPO_PATH]/.yarn/unplugged/prettier-npm-3.1.1-072c31ec21/node_modules/prettier/index.mjs:23186:10)
at file://[REPO_PATH]/.yarn/unplugged/prettier-npm-3.1.1-072c31ec21/node_modules/prettier/index.mjs:23203:12
at async loadPlugin (file://[REPO_PATH]/.yarn/unplugged/prettier-npm-3.1.1-072c31ec21/node_modules/prettier/index.mjs:23208:28)
at async Promise.all (index 1)
I first had this issue with the import sorting plugin but when i created an issue on their repo (https://github.com/IanVS/prettier-plugin-sort-imports/issues/140), I was provided with a easy fix. Simply unplugging the plugin and adding it to the plugins array in the prettier config with require.resolve(). That worked fine in isolation and format on save and linting worked fine, but the second I tried adding in the tailwind plugin it broke prettier’s format on save. I also tried using the require.resolve() trick on the prettier plugin and it did make the cannot find error go away
Currently the repo linked above has it setup with require.resolve() and the linting works but formatting on save does not. If you remove the prettier-plugin-tailwindcss from the plugins array, it will work again and format on save.
I assume this is the correct place for this issue, however let me know if this also needs to be cross-posted to prettier or yarnpkg.
Thanks for any insight as to what the problem is!
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 18
- Comments: 17 (1 by maintainers)
After an afternoon of searching, I finally found the perfect solution.
unplug the prettier-plugin-tailwindcss
for the .prettierrc.cjs
Remember to restart the prettier(because it will not hot-reload the new config). Simply, you can restart the IDE.
details are here https://dev.to/javien/how-to-use-prettier-plugin-with-yarn-pnp-in-vscode-4pf8
the issue persists
My temporary solution is to use a config file like this.
With this config
yarn prettier .command works fine.But the problem is prettier extension in VSCode gets infinitely loaded. (For more information on this, see prettier-vscode issue)
So I decided to temporarily replace “Format on save” with “format on pre-commit” by using
husky.I ended up creating a “node_modules” at the parent directory and placed “prettier” and “prettier-plugin-tailwindcss” inside it. If I had put them directly into the project’s node_modules folder, they would have been removed every time I ran “yarn install.” I’m using: Yarn PnP 4.1.0 prettier 3.1.1 prettier-plugin-tailwindcss 0.5.9 Windows
I found a way to use this plugin with Yarn 2
First you type this command
yarn unplug prettier-plugin-tailwindcssThen you can find the directory start with prettier-plugin-tailwindcss-virtual- in.yarn/unpluggedEdit your
.prettierrclike below linesBut I don’t want to use this with specific path. Are there any good solution?
I have found this also works for me as well although due to our use of the novel “zero-installs” feature I cannot use it as a workaround for now since it requires PNP.
This makes me think that it could be a peer dependency issue or something along the lines of that. If it works fine when not in PNP, but breaks in PNP and prettier and plugins are installed unplugged then maybe a peer dep is not being install that also needs to be unplugged? I’ll take a look at this later and see what I can find although im on vacation right now so a little tough.
I have this issue with yarn 4. This plugin breaks prettier “Format on save” and “Format document with…” command, although it works when prettier is ran from the terminal.
Any progress? Creating
node_modulesin the Yarn PNP project isn’t a good solution, I guess.