Skip to content

Support .prettierrc.js when type module is used in package.json #12701

@christoph-pflueger

Description

@christoph-pflueger

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:

  1. Set "type": "module" in package.json
  2. 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).

Metadata

Metadata

Assignees

Labels

locked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions