When using pnpm, one has to use require.resolve or import.meta.resolve in the .prettierrc.js configuration to resolve plugins. As I have type set to module in my package.json, .prettierrc.js throws an error.
Environments:
- Prettier Version: 2.6.2
- Running Prettier via: CLI
- Runtime: VS Code Linux
- Operating System: Ubuntu
- Prettier plugins (if any): prettier-plugin-import-sort
Steps to reproduce:
- Set "type": "module" in package.json
- Create a .prettierrc.js:
const config = {
tabWidth: 2,
plugins: [
import.meta.resolve('prettier-plugin-import-sort')
]
}
export default config
Expected behavior:
Should work.
Actual behavior:
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/dev/project/.prettierrc.js from /home/dev/.vscode/extensions/esbenp.prettier-vscode-9.5.0/node_modules/prettier/third-party.js not supported. .prettierrc.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules. Instead rename .prettierrc.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/dev/project/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
When using pnpm, one has to use require.resolve or import.meta.resolve in the .prettierrc.js configuration to resolve plugins. As I have type set to module in my package.json, .prettierrc.js throws an error.
Environments:
Steps to reproduce:
Expected behavior:
Should work.
Actual behavior:
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/dev/project/.prettierrc.js from /home/dev/.vscode/extensions/esbenp.prettier-vscode-9.5.0/node_modules/prettier/third-party.js not supported. .prettierrc.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules. Instead rename .prettierrc.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/dev/project/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).