-
-
Notifications
You must be signed in to change notification settings - Fork 456
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
Cannot use import
in prettier.config.js
with Prettier v3 and ESM
#3066
Comments
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 import tailwind from "prettier-plugin-tailwindcss";
/** @type {import("prettier").Options} */
export default {
printWidth: 80,
trailingComma: "all",
htmlWhitespaceSensitivity: "css",
plugins: [tailwind],
};
|
@zkulbeda I noticed the error message says |
Got it, yep that's the exact same error I'm getting. |
@michaelhays did you solve the problem? I didn't. We should open another issue if so |
I think it's the same issue as this one -- the error only happens for me when using ESM I was able to make it work for now by using CommonJS for this file instead, by naming it /** @type {import("prettier").Options} */
module.exports = {
semi: false,
singleQuote: true,
plugins: ['prettier-plugin-tailwindcss'],
} |
Hm, it also works with esm without import. So I suppose the problem isn't about esm /** @type {import("prettier").Options} */
export default {
printWidth: 80,
trailingComma: "all",
htmlWhitespaceSensitivity: "css",
plugins: ["prettier-plugin-tailwindcss"],
}; |
My 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. |
Getting the same error when on //prettier.config.js
/** @type {import('prettier').Config} */
module.exports = {
trailingComma: "all",
tabWidth: 2,
semi: false,
plugins: [require("prettier-plugin-tailwindcss")],
tailwindConfig: "./packages/configs/tailwind/tailwind.config.js",
tailwindAttributes: [
"modalStyles",
"titleStyles",
"subtitleStyles",
"iconStyles",
],
tailwindFunctions: ["clsx", "cn"],
} |
getting the save error with prettier@3 on vscode ["ERROR" - 09:47:27] Error handling text editor change
["ERROR" - 09:47:27] Invalid host defined options
TypeError: Invalid host defined options
at Object.<anonymous> (/Users/fengxing/miro/mironote/node_modules/prettier/index.cjs:600:23)
at Module.u._compile (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/loader.js:4:1271)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1243:10)
at Module.load (node:internal/modules/cjs/loader:1058:32)
at Module._load (node:internal/modules/cjs/loader:893:12)
at Function.f._load (node:electron/js2c/asar_bundle:2:13330)
at Function.l._load (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:127:28070)
at Function.p._load (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:127:25404)
at Function.u._load (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:91:22191)
at Module.require (node:internal/modules/cjs/loader:1082:19)
at g (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/loader.js:4:647)
at t.loadNodeModule (/Users/fengxing/.vscode/extensions/esbenp.prettier-vscode-9.19.0/dist/extension.js:1:2829)
at t.PrettierMainThreadInstance.import (/Users/fengxing/.vscode/extensions/esbenp.prettier-vscode-9.19.0/dist/extension.js:1:17760)
at t.ModuleResolver.getPrettierInstance (/Users/fengxing/.vscode/extensions/esbenp.prettier-vscode-9.19.0/dist/extension.js:1:5728)
at t.default.handleActiveTextEditorChanged (/Users/fengxing/.vscode/extensions/esbenp.prettier-vscode-9.19.0/dist/extension.js:1:10771)
at t.default.handleActiveTextEditorChangedSync (/Users/fengxing/.vscode/extensions/esbenp.prettier-vscode-9.19.0/dist/extension.js:1:10373)
at t.default.registerDisposables (/Users/fengxing/.vscode/extensions/esbenp.prettier-vscode-9.19.0/dist/extension.js:1:13531)
at /Users/fengxing/.vscode/extensions/esbenp.prettier-vscode-9.19.0/dist/extension.js:1:82528 |
I didn't try your exact config but had the issue with |
I'm experiencing the same problem as described above. My config file module.exports = {
"trailingComma": "es5",
"tabWidth": 4,
"semi": true,
"singleQuote": true,
"singleAttributePerLine": false,
"bracketSpacing": true,
"plugins": [
require("prettier-plugin-organize-attributes"),
],
} If |
I am working in a monorepo and trying to compose the prettier config. At root level I have this file: /** @type import('prettier').Config */
export default {
printWidth: 110,
endOfLine: 'auto',
singleQuote: true,
trailingComma: 'none'
}; In one of my apps within the monorepo I was attempting this import baseConfig from '../../prettier.config.mjs';
/** @type import('prettier').Config */
export default {
...baseConfig,
plugins: ['prettier-plugin-tailwindcss'],
tailwindConfig: 'tailwind.config.mjs',
tailwindFunctions: ['cn']
}; 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 |
Thank you, when I do this it runs smoothly. const config = {
plugins: ['prettier-plugin-tailwindcss'],
printWidth: 100,
tabWidth: 4,
useTabs: true,
semi: true,
singleQuote: true,
jsxSingleQuote: true,
trailingComma: 'all',
bracketSameLine: true,
arrowParens: 'always',
endOfLine: 'lf',
};
module.exports = config; |
Wasn't able to get any of the above working. We also use pretty-quick so that may be the cause. Reverting to |
Bug: unable to use ESM, see: prettier/prettier-vscode#3066
For me when using NextJS 13 with latest prettier, neither // prettier.config.ts
/** @type {import('prettier').Config} */
module.exports = {
semi: true,
singleQuote: true,
plugins: ['prettier-plugin-tailwindcss'],
}; |
I have been searching for a solution to this for ages! Thank you so much for sharing, this worked for me! |
Changing |
For me, Using module.exports = {
plugins: [require.resolve("prettier-plugin-organize-imports")],
}; |
Thanks, solved it here by removing the require |
doesn't work for me:
|
Restarting the VSCode after require.resolve() was applied fixed my issue. |
I had the same problem and it was fixed by restarting vscode |
This is the solution for prettier@^3 |
The solution for me was to remove PS: Whenever prettier related stuff is changed, is better to reload the VSCode to get the new changes. |
Using the ESM config example straight from the docs (from my default config in my home directory) throws this error:
|
@vincerubinetti Mostly you did not configure something right. Extension basically just does what is supposed to do. |
@iamandrewluca Nope. My extension has all the default settings (no |
Crash description + reproduction@ntotten @sosukesuzuki The Prettier VS Code extension (
Reproduction repo: https://github.com/karlhorky/prettier-vscode-10-1-0-esm-crash
CauseThis 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 ( $ pnpm prettier --version
2.8.8
$ pnpm prettier index.js --write
[error] Invalid configuration file `index.js`: require() of ES Module /Users/k/p/prettier-vscode-10-1-0-esm-crash/prettier.config.js from /Users/k/p/prettier-vscode-10-1-0-esm-crash/node_modules/.pnpm/[email protected]/node_modules/prettier/third-party.js not supported.
[error] Instead change the require of prettier.config.js in /Users/k/p/prettier-vscode-10-1-0-esm-crash/node_modules/.pnpm/[email protected]/node_modules/prettier/third-party.js to a dynamic import() which is available in all CommonJS modules. WorkaroundAdd Prettier v3 to your dependencies in every project 😬 {
"type": "module",
"main": "index.js",
"dependencies": {
+ "prettier": "3.2.5"
}
} Fix@ntotten would you accept a new PR upgrading to Prettier v3 by default in the Prettier VS Code extension? |
Just saw that this issue has "Prettier v3" in the title, whereas the VS Code extension is not yet on Prettier v3 internally (hopefully soon!). I created a new issue over here: |
This issue has been labeled as stale due to inactivity. Reply to keep this issue open. |
I guess this hasn't been resolved yet. |
Also the bot malfunctions. You replied and the [stale] is still there |
If you are using yarn pnp the following fixed it for me: |
This issue has been labeled as stale due to inactivity. Reply to keep this issue open. |
Probably not stale |
For me, it seems to work fine. Prettier v3 and ESM 2024-07-24.09-53-38.mp4 |
@iamandrewluca look through some of the other comments eg. the one I posted above - your particular setup may work, but there are still issues here, as far as I last checked. |
Uninstalled prettier, and yes, I can confirm that "Format Document" fails with the same error. |
@prettier can you please fix your bot and also pin this issue so the bot doesn’t come back? |
@sosukesuzuki Is it possible to load config in worker? The main thread should not need the config. |
Go to VS Code settings (UI). Search for prettier path. Update it to |
For those having issues when specifying plugins using a string ( To fix this, explicitly import the plugin with // File prettier.config.cjs
/** @type {import('prettier').Options} */
module.exports = {
plugins: [require.resolve('a-prettier-plugin')]
}; See also #3104 (comment) |
Summary
Format fails if
prettier.config.js
is written in ESM and containsimport
for plugins.Github Repository to Reproduce Issue
https://github.com/risu729/prettier-test
Steps To Reproduce:
prettier.config.js
with{"type": "module"}
containingimport
like below.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 oftype
inpackage.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
,For
prettier.config.mjs
, the error object seems empty.The text was updated successfully, but these errors were encountered: