prettier-vscode: Cannot use `import` in `prettier.config.js` with Prettier v3 and ESM
Summary
Format fails if prettier.config.js
is written in ESM and contains import
for plugins.
Github Repository to Reproduce Issue
https://github.com/risu729/prettier-test
Steps To Reproduce:
- Create
prettier.config.js
with{"type": "module"}
containingimport
like below.
import organizeImports from "prettier-plugin-organize-imports";
export default {
plugins: [organizeImports],
};
- Run format for a file.
Expected result
Format the file successfully.
Actual result
Failed with error: Error resolving prettier configuration for $path
However, npm exec prettier --write .
runs successfully.
If I change the filename to prettier.config.mjs
, regardless of type
in package.json
, prettier fails.
Additional information
VS Code Version: Version: 1.81.0-insider (user setup) Commit: 79ec05cb73486ad2d57a57a5f138b094d21c8644 Date: 2023-07-06T23:18:22.645Z Electron: 22.3.14 ElectronBuildId: 21893604 Chromium: 108.0.5359.215 Node.js: 16.17.1 V8: 10.8.168.25-electron.0 OS: Windows_NT x64 10.0.23493
Prettier Extension Version: 9.19.0
OS and version: Windows 11 Home Insider Preview 22H2
Prettier Log Output
For prettier.config.js
,
["INFO" - 1:01:56 PM] Formatting file:///c%3A/Users/taku/Documents/GitHub/prettier-test/prettier.config.js
["DEBUG" - 1:01:56 PM] Local prettier module path: 'c:\Users\taku\Documents\GitHub\prettier-test\node_modules\prettier\index.cjs'
["ERROR" - 1:01:56 PM] Error resolving prettier configuration for c:\Users\taku\Documents\GitHub\prettier-test\prettier.config.js
["ERROR" - 1:01:56 PM] require() of ES Module c:\Users\taku\Documents\GitHub\prettier-test\prettier.config.js from C:\Users\taku\Documents\GitHub\prettier-test\node_modules\.pnpm\prettier@3.0.0\node_modules\prettier\internal\internal.mjs not supported.
Instead change the require of prettier.config.js in C:\Users\taku\Documents\GitHub\prettier-test\node_modules\.pnpm\prettier@3.0.0\node_modules\prettier\internal\internal.mjs to a dynamic import() which is available in all CommonJS modules.
Error [ERR_REQUIRE_ESM]: require() of ES Module c:\Users\taku\Documents\GitHub\prettier-test\prettier.config.js from C:\Users\taku\Documents\GitHub\prettier-test\node_modules\.pnpm\prettier@3.0.0\node_modules\prettier\internal\internal.mjs not supported.
Instead change the require of prettier.config.js in C:\Users\taku\Documents\GitHub\prettier-test\node_modules\.pnpm\prettier@3.0.0\node_modules\prettier\internal\internal.mjs to a dynamic import() which is available in all CommonJS modules.
at f._load (node:electron/js2c/asar_bundle:2:13330)
at module.exports (file:///C:/Users/taku/Documents/GitHub/prettier-test/node_modules/.pnpm/prettier@3.0.0/node_modules/prettier/internal/internal.mjs:173:34)
at loadJsSync2 (file:///C:/Users/taku/Documents/GitHub/prettier-test/node_modules/.pnpm/prettier@3.0.0/node_modules/prettier/internal/internal.mjs:5394:22)
at loadJs2 (file:///C:/Users/taku/Documents/GitHub/prettier-test/node_modules/.pnpm/prettier@3.0.0/node_modules/prettier/internal/internal.mjs:5404:16)
at async Explorer.loadFileContent (file:///C:/Users/taku/Documents/GitHub/prettier-test/node_modules/.pnpm/prettier@3.0.0/node_modules/prettier/internal/internal.mjs:5763:18)
at async Explorer.createCosmiconfigResult (file:///C:/Users/taku/Documents/GitHub/prettier-test/node_modules/.pnpm/prettier@3.0.0/node_modules/prettier/internal/internal.mjs:5770:29)
at async Explorer.loadSearchPlace (file:///C:/Users/taku/Documents/GitHub/prettier-test/node_modules/.pnpm/prettier@3.0.0/node_modules/prettier/internal/internal.mjs:5752:16)
at async Explorer.searchDirectory (file:///C:/Users/taku/Documents/GitHub/prettier-test/node_modules/.pnpm/prettier@3.0.0/node_modules/prettier/internal/internal.mjs:5742:31)
at async run (file:///C:/Users/taku/Documents/GitHub/prettier-test/node_modules/.pnpm/prettier@3.0.0/node_modules/prettier/internal/internal.mjs:5728:26)
at async Explorer.search (file:///C:/Users/taku/Documents/GitHub/prettier-test/node_modules/.pnpm/prettier@3.0.0/node_modules/prettier/internal/internal.mjs:5723:16)
at async Module.resolveConfigFile (file:///C:/Users/taku/Documents/GitHub/prettier-test/node_modules/.pnpm/prettier@3.0.0/node_modules/prettier/index.mjs:20333:18)
For prettier.config.mjs
, the error object seems empty.
["INFO" - 2:29:40 PM] Formatting file:///c%3A/Users/taku/Documents/GitHub/prettier-test/prettier.config.mjs
["DEBUG" - 2:29:40 PM] Local prettier module path: 'c:\Users\taku\Documents\GitHub\prettier-test\node_modules\prettier\index.cjs'
["DEBUG" - 2:29:40 PM] Using prettier version 3.0.0
["ERROR" - 2:29:40 PM] Invalid prettier configuration file detected.
{}
["ERROR" - 2:29:40 PM] Invalid prettier configuration file detected. See log for details.
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 42
- Comments: 37 (3 by maintainers)
Commits related to this issue
- Downgrade prettier while we wait for š ±ļøSCode to support it Issue for ref: https://github.com/prettier/prettier-vscode/issues/3066 — committed to typehero/typehero by Hacksore a year ago
- Downgrade prettier while we wait for š ±ļøSCode to support it (#259) Issue for ref: https://github.com/prettier/prettier-vscode/issues/3066 — committed to typehero/typehero by Hacksore a year ago
- Add prettier Bug: unable to use ESM, see: https://github.com/prettier/prettier-vscode/issues/3066 — committed to coxde/home by coxde 10 months ago
- Downgrade prettier while we wait for š ±ļøSCode to support it (#259) Issue for ref: https://github.com/prettier/prettier-vscode/issues/3066 — committed to growno1127/ticketbro by growno1127 a year ago
I think itās the same issue as this one ā the error only happens for me when using ESM
import
.I was able to make it work for now by using CommonJS for this file instead, by naming it
prettier.config.cjs
:@tebuevd
I didnāt try your exact config but had the issue with
plugins: [require("prettier-plugin-tailwindcss")],
. Changing it toplugins: ['prettier-plugin-tailwindcss'],
workedMy prettier config is in package.json. 2.8.8 works fine. Upgrading to prettier 3.0 makes this plugin fail with following error:
Running prettier from command line works fine.
For me when using NextJS 13 with latest prettier, neither
.cjs
or.js
worked but changing config file to.ts
fixed the issue.Oh, seems plugin canāt load new config, and on every attempt tries import old file. I use
Developer: Restart Extension Host
to reload config and itās working. The import error disappear, but there is another one I deleted node_modules and install depencies to wipe my changes.pnpm prettier --write .
runs without errorI have been searching for a solution to this for ages! Thank you so much for sharing, this worked for me!
Crash description + reproduction
@ntotten @sosukesuzuki The Prettier VS Code extension (
esbenp.prettier-vscode@10.1.0
) currently crashes when:prettier
dependency installed inpackage.json
"type": "module"
inpackage.json
prettier.config.js
with ESMReproduction repo: https://github.com/karlhorky/prettier-vscode-10-1-0-esm-crash
Cause
This is because the VS Code extension uses the old Prettier v2 internally - because the PR from @sosukesuzuki upgrading to Prettier v3 by default was reverted:
ā¦and Prettier v2 (
prettier@2.8.8
) also breaks with a similar error:Workaround
Add Prettier v3 to your dependencies in every project š¬
Fix
@ntotten would you accept a new PR upgrading to Prettier v3 by default in the Prettier VS Code extension?
Cosmiconfig uses require as fallback when esm module import throws error. I manually edited prettier in node_modules to throw instead of fallback and got this error
My
prettier.config.js
Seems prettier vscode plugin canāt resolve imports from prettier config. Iām using pnpm as package manager and got same error if using imports from my config. If I remove imports from config, the error will disappearThis error appears when cosmiconfig import ends up with error.Also the bot malfunctions. You replied and the [stale] is still there
Restarting the VSCode after require.resolve() was applied fixed my issue.
Iām getting an slight different error: A dynamic import callback was not specified. Is related with a project configured as module type
In this repo.
I dont use import or require in the prettier config. Its just json (.prettierrc):
The project is configured as type: module in package.json
When i change the project type to commonjs, the config file is loaded normally and prettier works
Error callstack
Thanks, solved it here by removing the require
For me,
prettier.config.cjs
withplugins: [require.resolve('plugin-name')]
like below worked for both the vscode extension and CLI.Using
prettier.config.ts
resolved the error; however, the plugin did not work. Also,plugins: ['plugin-name']
andplugins: [require('plugin-name')]
didnāt throw errors, but the plugin didnāt work.I am working in a monorepo and trying to compose the prettier config. At root level I have this file:
In one of my apps within the monorepo I was attempting this
This was failing with the following error message
Only after researching the issue thoroughly I stumbled upon this thread and noticed that I have to remove the
import
for now which makes the config not composable, sadly.Got it, yep thatās the exact same error Iām getting.
no:
Only URLs with a scheme in: file and data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'
BTW: Iām using yarn pnp mode
Changing
plugins: ['plugin-name']
toplugins: [require.resolve('plugin-name')]
works for me in both vscode and precommitThank you, when I do this it runs smoothly.