Skip to content
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

prettier-plugin-sort-imports with prettier >=3 #251

Open
sabatorussonttdata opened this issue Aug 30, 2023 · 7 comments
Open

prettier-plugin-sort-imports with prettier >=3 #251

sabatorussonttdata opened this issue Aug 30, 2023 · 7 comments

Comments

@sabatorussonttdata
Copy link

Environment
Prettier version**: 3.0.3

  • node version 18
  • package manager: yarn@2
  • IDE: VScode

Describe the bug

with version >=3 prettier-plugin-sort-imports doesn't work
while with version 2.0.8 it works correctly.
How come ?

@d0whc3r
Copy link

d0whc3r commented Sep 1, 2023

You have 3 more issues talking about this, search them

TL;DR:
#240
#245
#244

@hrumhurum
Copy link

A workaround is to use Javascript config format .prettierrc.js instead of JSON config format .prettierrc: #131 (comment)

@notpeelz
Copy link

notpeelz commented Sep 7, 2023

Nothing works for me.

Package manager: Yarn (berry, 3.6.x)
Node: 20.5.1
IDE: vscode, using the prettier-vscode extension

My working configuration (Prettier 2.8.8)

package.json

{
  "type": "module",
  "packageManager": "yarn@3.6.0",
  "dependencies": {
    "@trivago/prettier-plugin-sort-imports": "^4.2.0",
    "prettier": "^2.8.8"
  }
}

.prettierrc.cjs

/** @type {import("prettier").Config} */
module.exports = {
  plugins: [require("@trivago/prettier-plugin-sort-imports")],
  singleQuote: false,
  jsxSingleQuote: false,
  arrowParens: "always",
  trailingComma: "all",
  bracketSameLine: false,
  importOrder: ["^node:", "^(?![./]).*$", "^[./]"],
  importOrderSeparation: true,
  importOrderSortSpecifiers: true,
};
After upgrading: Prettier 2.8.8 -> Prettier 3.0.3, Yarn 3.6.0 -> Yarn 3.6.3

vscode-prettier

["INFO"] Formatting file:///home/peelz/path/to/project/src/main.mts
["ERROR"] Invalid prettier configuration file detected.
{}
["ERROR"] Invalid prettier configuration file detected. See log for details.

prettier cli (works)

$ yarn prettier --version
3.0.3
$ yarn prettier -w ./src/main.mts
src/main.mts 158ms
Things I've tried
  1. plugins: ["@trivago/prettier-plugin-sort-imports"] (without the require()):

    prettier cli

    $ yarn prettier -w ./src/main.mts
    [error] Cannot find package '@trivago/prettier-plugin-sort-imports' imported from /home/peelz/path/to/project/noop.js
    

    The error is the same with JSON config formats.

    vscode-prettier

    ["INFO"] Using config file at '/home/peelz/path/to/project/.prettierrc.cjs'
    ["ERROR"] Error handling text editor change
    ["ERROR"] Error resolve node module '@trivago/prettier-plugin-sort-imports'
    Error: Error resolve node module '@trivago/prettier-plugin-sort-imports'
    	at n (/home/peelz/.local/share/Code/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:2718)
    	at /home/peelz/.local/share/Code/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:3068
    	at Array.map (<anonymous>)
    	at t.resolveConfigPlugins (/home/peelz/.local/share/Code/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:3003)
    	at t.ModuleResolver.resolveConfig (/home/peelz/.local/share/Code/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:7290)
    	at async t.default.getSelectors (/home/peelz/.local/share/Code/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:11562)
    	at async t.default.handleActiveTextEditorChanged (/home/peelz/.local/share/Code/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:10924)
    
  2. plugins: [(await import("@trivago/prettier-plugin-sort-imports")).default] in .prettierrc.mjs/.prettierrc.js:

    prettier cli

    $ yarn prettier -w ./src/main.mts
    [warn] Ignored unknown option { importOrder: ["^node:", "^(?![./]).*$", "^[./]"] }.
    [warn] Ignored unknown option { importOrderSeparation: true }.
    [warn] Ignored unknown option { importOrderSortSpecifiers: true }.
    src/main.mts 213ms
    

    vscode-prettier

    ["INFO"] Formatting file:///home/peelz/path/to/project/src/main.mts
    ["ERROR"] Error resolving prettier configuration for /home/peelz/path/to/project/src/main.mts
    ["ERROR"] require() of ES Module /home/peelz/path/to/project/.prettierrc.mjs not supported.
    Instead change the require of /home/peelz/path/to/project/.prettierrc.mjs to a dynamic import() which is available in all CommonJS modules.
    Error [ERR_REQUIRE_ESM]: require() of ES Module /home/peelz/path/to/project/.prettierrc.mjs not supported.
    Instead change the require of /home/peelz/path/to/project/.prettierrc.mjs to a dynamic import() which is available in all CommonJS modules.
        at f._load (node:electron/js2c/asar_bundle:2:13330)
        at require$$0.Module._load (/home/peelz/path/to/project/.pnp.cjs:15413:31)
        at module.exports (file:///home/peelz/path/to/project/.yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/internal/internal.mjs:173:34)
        at loadJsSync2 (file:///home/peelz/path/to/project/.yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/internal/internal.mjs:5418:22)
        at loadJs2 (file:///home/peelz/path/to/project/.yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/internal/internal.mjs:5428:16)
        at async Explorer.loadFileContent (file:///home/peelz/path/to/project/.yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/internal/internal.mjs:5787:18)
        at async Explorer.createCosmiconfigResult (file:///home/peelz/path/to/project/.yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/internal/internal.mjs:5794:29)
        at async Explorer.loadSearchPlace (file:///home/peelz/path/to/project/.yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/internal/internal.mjs:5776:16)
        at async Explorer.searchDirectory (file:///home/peelz/path/to/project/.yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/internal/internal.mjs:5766:31)
        at async run (file:///home/peelz/path/to/project/.yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/internal/internal.mjs:5752:26)
        at async Explorer.search (file:///home/peelz/path/to/project/.yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/internal/internal.mjs:5747:16)
        at async Module.resolveConfigFile (file:///home/peelz/path/to/project/.yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/index.mjs:20044:18)
    

    This issue is tracked here: Cannot use import in prettier.config.js with Prettier v3 and ESM prettier/prettier-vscode#3066
    Some people suggest using plugins: ["plugin-name"] instead of require()/import(), but as you can see, that doesn't work (see 1.)

@rizbud
Copy link

rizbud commented Sep 15, 2023

this solution works for me #245 (comment)

@well1791
Copy link

this solution works for me #245 (comment)

// .prettierrc
plugins: ['@trivago/prettier-plugin-sort-imports']

@LucasHBG
Copy link

This issue still remains in prettier 3.1.1
I tried:

  • Using .prettierrc
  • Changing from .prettierrc to .prettierrc.js
  • Changing from .prettierrc to .prettierrc.config.js
  • Tried to add it in plugins with the solution from Prettier > 3 doesn't work #245 (comment) , require() and import().

but none seems to work

@dinumihnea
Copy link

dinumihnea commented Feb 16, 2024

this solution works for me #245 (comment)

// .prettierrc
plugins: ['@trivago/prettier-plugin-sort-imports']

Thank you a lot, saved my day.
This should be in the Readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants