Repository navigation
Closed
Labels
[Status] BlockedUsed to indicate that a current effort isn't able to move forward[Tool] WP Scripts/packages/scripts[Type] BugAn existing feature does not function as intended
Description
Describe the bug
Error with the prettier dependency while running the lint script from @wordpress/scripts after a new npm install.
To reproduce
Steps to reproduce the behavior:
- Create a project and add the
@wordpress/scriptsdev dependency
> mkdir scripts-test \
&& cd scripts-test \
&& npm init -y && npm i -D @wordpress/scripts
- Add the script to the package.json:
"lint:js": "wp-scripts lint-js" - Create a js file and run the lint to see it is working.
> echo 'const x = 1;' >index.js \
&& npm run lint:js
- Checking the prettier dependency in this moment:
> npm ls prettier
scripts-test@1.0.0 /Users/renatho/Downloads/scripts-test
└─┬ @wordpress/scripts@7.1.3
├─┬ @wordpress/eslint-plugin@4.0.0
│ └── prettier@npm:wp-prettier@1.19.1 deduped
└── prettier@npm:wp-prettier@1.19.1
- You can make a
git inithere and commit thepackage-lockto see the next changes. - Remove the
node_modulesfolder, reinstall the packages and run the lint script. The new install will update (unexpectedly) ourpackage-lock.jsonand It stops to work because it doesn't find theprettier. You will see the errorError: Cannot find module 'prettier'
> rm -rf node_modules \
&& npm i \
&& npm run lint:js
- Check the prettier dependency to understand what happened (you can also see the
package-lockdiff):
> npm ls prettier
scripts-test@1.0.0 /Users/renatho/Downloads/scripts-test
└─┬ @wordpress/scripts@7.1.3
├─┬ @wordpress/eslint-plugin@4.0.0
│ └── UNMET PEER DEPENDENCY prettier@npm:wp-prettier@1.19.1
└── prettier@npm:wp-prettier@1.19.1
Expected behavior
Install the @wordpress/scripts and after other npm install it continues to work properly while running the npm run lint:js.
Version (please complete the following information):
- A clean project with only the
@wordpress/scriptsinstalled. Reproduced in7.1.3and8.0.1
Desktop (please complete the following information):
- OS: macOS Catalina
- NPM 6.14.4 / Node 10.17.0
Reacted by Kaspars Dambis
Metadata
Metadata
Assignees
Labels
[Status] BlockedUsed to indicate that a current effort isn't able to move forward[Tool] WP Scripts/packages/scripts[Type] BugAn existing feature does not function as intended
Activity
gziolo commentedon Apr 26, 2020
It’s a know issue and I don’t think we can help much about it. In my experience running
npm installtwice resolves it, so it looks like there is something weird happening on the npm side when using aliases. There are two things that might help:npm iagain when it happens or installing the aliased Prettier fork as a dependency of the projectrenatho commentedon Apr 26, 2020
Thank you for your answer with the suggestions @gziolo !!
For now, what we are using as a workaround is to install the prettier fork as a direct dependency. :)
And I tested running the
npm iagain, but it didn't solve for me :(For me, after the error happens changing the
package-lock, it doesn't work more unless we remove the package-lock to reinstall.gziolo commentedon Apr 26, 2020
@renatho thanks for testing and sharing what works for you 👍
add aliased wp-prettier as direct dependency (see: WordPress/gutenber…
Switch to wp-env (#46)
ndiego commentedon Nov 10, 2020
I just ran into this randomly as well. I had been using the lint command with no issues for awhile, and then all the sudden I got the error. If you remove
package-lock.jsonfile, delete thenode_modulesfolder and then runnpm iagain, the error went away for me.gziolo commentedon Dec 19, 2020
The best way to address this issue would be to stop using a npm alias for Prettier fork and use the official Prettier. However we would have to change WordPress coding styles for JavaScript to reflect how Prettier removes spaces around brackets.
renatho commentedon Dec 21, 2020
I imagine it would have a big impact. But it can be a good option for those who want to avoid this issue by replacing this standard, installing the dependency directly in their projects. :)
gziolo commentedon Dec 21, 2020
It only seems like a big impact on the vast majority of projects. If you don't have hundreds of open PRs in the repository then it all boils down to running
wp-scripts format-jsonce and you are all set.[-]Prettier not found running lint from @wordpress/scripts[/-][+]Scripts: Prettier fork not found when running lint-js[/+]ockham commentedon Mar 5, 2021
@jsnajdr @tyxla et al -- Have we seen any similar errors with the
prettierfork in Calypso? If so, are there any known "permanent" fixes/workarounds?gziolo commentedon Mar 8, 2021
They have it installed in the top-level
package.jsonhttps://github.com/Automattic/wp-calypso/blob/1718f43e78277d06f16df1bc550917dfb7d0c9c9/package.json#L281
Gutenberg does it, too. This resolves the issue but it shouldn't be necessary if one of the sub packages does it already. Another workaround is to run
npm installtwice, but it's very confusing.2 remaining items
chrisvanpatten commentedon Mar 23, 2021
@gziolo that would be fantastic. It would make things so much easier… this bug has been persistently annoying 😅
gziolo commentedon Mar 23, 2021
My proposal needs to wait until we sort out how we want to use TypeScript in Gutenberg:
https://make.wordpress.org/core/2021/03/18/proposal-native-typescript-support-in-gutenberg/
gziolo commentedon Sep 10, 2021
The same issue was reported in #34622. The context was
@wordpress/scriptbut the solution was also installing the npm alias for Prettier in the project.kasparsd commentedon Sep 15, 2021
I created this test repository https://github.com/xwp/gutenberg-issue-21872-scripts which attempts to isolate the issue and require just
@wordpress/scriptsat^18.0.1and runsnpm installfollowed bywp-scripts lint-jsusing different versions of Node and different package lock files generated by different versions of Node. See the GitHub action output or the test matrix logs (as GitHub action reports expire after some time).The results are all over the place as it appears to be an issue with Node and/or npm instead of these packages.
gziolo commentedon Sep 17, 2021
@kasparsd, thank you for doing a very in-depth investigation. That was my understanding, too. That's why I proposed we stop using the aliased fork and go with the official Prettier to save folks from all the hassle that it creates, even if that is independent from the WordPress packages.
Remove linting from ci due error with prettier not being found. See W…
Update the 01-basic-esnext block to use block.json (#149)
Fix WP prettier issue
Attempt to fix issue with prettier
gziolo commentedon Dec 23, 2021
I opened #37607 to start making the official proposal to switch to the original Prettier in the most recent 2.5 version.
Update the 01-basic-esnext block to use block.json (#149)
gziolo commentedon Dec 20, 2024
I tested it again with Node v22.7.0 and npm 10.9.0. I scaffolded the block with
npx @wordpress/create-block@latestto test it. It installed@wordpress/scriptsv30.7.0.I can't reproduce it anymore, so it must have been fixed upstream in the npm client.