prettier-plugin-blade icon indicating copy to clipboard operation
prettier-plugin-blade copied to clipboard

[Bug]: `Cannot require() ES Module ~ standalone.mjs in a cycle` when Node >=v22.12.0 with Prettier >= 3.5.0

Open utqinadhif opened this issue 9 months ago • 22 comments

Description

after update from 1.15.2 to 1.15.3, prettier not working with caption Cannot require() ES Module /media/hdd/local/project/php/l11-sim/node_modules/prettier/standalone.mjs in a cycle. (from /media/hdd/local/project/php/l11-sim/node_modules/@prettier/plugin-php/standalone.js)

Expected Behavior

i want to normal again

Actual Behavior

this plugin not work after update

Additional Context

version: 1.15.3 OS: linux debian 12

utqinadhif avatar Feb 17 '25 01:02 utqinadhif

currently downgraded to get going: "@shufo/prettier-plugin-blade": "=1.14.1",

npm update @shufo/prettier-plugin-blade

rishabkapadia avatar Feb 17 '25 16:02 rishabkapadia

Sorry I could not reproduce the error with latest version with prettier 3.5.1

$ node -v
v22.11.0

$ npm list | grep prettier
├── @shufo/[email protected]
└── [email protected]

$ cat package.json
{
  "devDependencies": {
    "@shufo/prettier-plugin-blade": "^1.15.3",
    "prettier": "^3.5.1"
  }
}

$ cat .prettierrc.json
{
  "plugins": ["@shufo/prettier-plugin-blade"],
  "overrides": [
    {
      "files": ["*.blade.php"],
      "options": {
        "parser": "blade",
        "tabWidth": 4
      }
    }
  ]
}


$ ./node_modules/.bin/prettier test.blade.php
@extends('frontend.layouts.app')
@section('title') foo
@endsection
@section('content')
    <section id="content">
        <div class="container mod-users-pd-h">
            <div class="pf-user-header">
                <div></div>
                <p>@lang('users.index')</p>
            </div>
            <div class="pf-users-branch">
                <ul class="pf-users-branch__list">
                    @foreach ($users as $user)
                        <li>
                            <img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow">
                            {{ link_to_route('frontend.users.user.show', $users['name'], $users['_id']) }}
                        </li>
                    @endforeach
                </ul>
                <div class="pf-users-branch__btn">
                    @can('create', App\Models\User::class)
                        {!! link_to_route('frontend.users.user.create', __('users.create'), [1, 2, 3], ['class' => 'btn']) !!}
                    @endcan
                </div>
            </div>
        </div>
    </section>
@endsection
@section('footer')
@stop

@utqinadhif @rishabkapadia Can you give how to reproduce the error?

shufo avatar Feb 18 '25 05:02 shufo

macOS 14.7.2 PHP 8.3.14

node -v
v23.6.1

npm -v
11.1.0

./node_modules/.bin/prettier resources/views/components/guest/nav.blade.php
[error] Cannot require() ES Module ../Sites/webapp/node_modules/prettier/standalone.mjs in a cycle. (from ../Sites/webapp/node_modules/@prettier/plugin-php/standalone.js)

cat .prettierrc
{
  "plugins": ["@shufo/prettier-plugin-blade"],
  "overrides": [
    {
      "files": ["*.blade.php"],
      "options": {
        "parser": "blade",
        "tabWidth": 4
      }
    }
  ]
}

cat package.json | grep prettier
    "@shufo/prettier-plugin-blade": "^1.15.3",
    "prettier": "^3.5.1",

cat package.json
{
  "private": true,
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "prepare": "husky"    
  },
  "devDependencies": {
    "@shufo/prettier-plugin-blade": "^1.15.3",
    "@tailwindcss/forms": "^0.5.7",
    "alpinejs": "^3.13.3",
    "autoprefixer": "^10.4.16",
    "axios": "^1.6",
    "husky": "^9.0.0",
    "laravel-vite-plugin": "^1.0.1",
    "postcss": "^8.4.33",
    "postcss-import": "^16.0.0",
    "prettier": "^3.5.1",
    "tailwindcss": "^3.4.1",
    "vite": "^6.1.0"
  }
}

also tried:

rm -rf node_modules package-lock.json 
npm install

rishabkapadia avatar Feb 18 '25 07:02 rishabkapadia

macOS 14.7.2 PHP 8.3.14

node -v
v23.6.1

npm -v
11.1.0

./node_modules/.bin/prettier resources/views/components/guest/nav.blade.php
[error] Cannot require() ES Module ../Sites/webapp/node_modules/prettier/standalone.mjs in a cycle. (from ../Sites/webapp/node_modules/@prettier/plugin-php/standalone.js)

cat .prettierrc
{
  "plugins": ["@shufo/prettier-plugin-blade"],
  "overrides": [
    {
      "files": ["*.blade.php"],
      "options": {
        "parser": "blade",
        "tabWidth": 4
      }
    }
  ]
}

cat package.json | grep prettier
    "@shufo/prettier-plugin-blade": "^1.15.3",
    "prettier": "^3.5.1",

cat package.json
{
  "private": true,
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "prepare": "husky"    
  },
  "devDependencies": {
    "@shufo/prettier-plugin-blade": "^1.15.3",
    "@tailwindcss/forms": "^0.5.7",
    "alpinejs": "^3.13.3",
    "autoprefixer": "^10.4.16",
    "axios": "^1.6",
    "husky": "^9.0.0",
    "laravel-vite-plugin": "^1.0.1",
    "postcss": "^8.4.33",
    "postcss-import": "^16.0.0",
    "prettier": "^3.5.1",
    "tailwindcss": "^3.4.1",
    "vite": "^6.1.0"
  }
}

also tried:

rm -rf node_modules package-lock.json 
npm install

Thanks

utqinadhif avatar Feb 18 '25 08:02 utqinadhif

Uh I understand, it occurs when combination of node >=v23 with prettier-plugin-blade latest version.

$ node -v
v23.6.1

$ ./node_modules/.bin/prettier test.blade.php
[error] Cannot require() ES Module /home/shuhei/develop/prettier-plugin-blade-test6/node_modules/prettier/standalone.mjs in a cycle. (from /home/shuhei/develop/prettier-plugin-blade-test6/node_modules/@prettier/plugin-php/standalone.js)

Node v22 with latest version was ok.

shufo avatar Feb 18 '25 10:02 shufo

node -v ─╯ v22.14.0

./node_modules/.bin/prettier /media/hdd/local/project/php/l11-sim/resources/views/pdf/display/apm/print/sep.blade.php ─╯ [error] Cannot require() ES Module /media/hdd/local/project/php/l11-sim/node_modules/prettier/standalone.mjs in a cycle. (from /media/hdd/local/project/php/l11-sim/node_modules/@prettier/plugin-php/standalone.js)

In my case, i use v22.14.0 but error,

utqinadhif avatar Feb 18 '25 10:02 utqinadhif

Oh i know, before today, before i update my node, the prettier work well, may be error occurs on >=22.14.0

utqinadhif avatar Feb 18 '25 11:02 utqinadhif

Uh I understand, it occurs when combination of node >=v23 with prettier-plugin-blade latest version.

$ node -v v23.6.1

$ ./node_modules/.bin/prettier test.blade.php [error] Cannot require() ES Module /home/shuhei/develop/prettier-plugin-blade-test6/node_modules/prettier/standalone.mjs in a cycle. (from /home/shuhei/develop/prettier-plugin-blade-test6/node_modules/@prettier/plugin-php/standalone.js) Node v22 with latest version was ok.

i just tried it with node 22 (& plugin 1.15.3), its not working. i'm on v23 for quite some time (its highly unlikely the cause). faced issues recently (went from 1.13.4 to 1.15.3).

Got it working by downgrading to 1.13.4 with node 23

rishabkapadia avatar Feb 18 '25 11:02 rishabkapadia

@utqinadhif @rishabkapadia Thanks.

https://github.com/shufo/prettier-plugin-blade/issues/311#issuecomment-2665172134 was not corret. It seems above node >=v22.12.0 got error.

$ node -v
v22.12.0
$ ./node_modules/.bin/prettier test.blade.php
[error] Cannot require() ES Module /home/shuhei/develop/prettier-plugin-blade-test6/node_modules/prettier/standalone.mjs in a cycle. (from /home/shuhei/develop/prettier-plugin-blade-test6/node_modules/@prettier/plugin-php/standalone.js)

v22.11.0 is ok

$ node -v
v22.11.0

$ ./node_modules/.bin/prettier test.blade.php
@extends('frontend.layouts.app')
@section('title') foo
@endsection
@section('content')
    <section id="content">
        <div class="container mod-users-pd-h">
            <div class="pf-user-header">
                <div></div>
                <p>@lang('users.index')</p>
            </div>
            <div class="pf-users-branch">
                <ul class="pf-users-branch__list">
                    @foreach ($users as $user)
                        <li>
                            <img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow">
                            {{ link_to_route('frontend.users.user.show', $users['name'], $users['_id']) }}
                        </li>
                    @endforeach
                </ul>
                <div class="pf-users-branch__btn">
                    @can('create', App\Models\User::class)
                        {!! link_to_route('frontend.users.user.create', __('users.create'), [1, 2, 3], ['class' => 'btn']) !!}
                    @endcan
                </div>
            </div>
        </div>
    </section>
@endsection
@section('footer')
@stop

shufo avatar Feb 18 '25 11:02 shufo

that worked. thank you.

note: if you are using brew, i was not able to install a specific version. instead it kept installing the latest patch (v22.13.1). hence had to install 22.11 via nvm

rishabkapadia avatar Feb 18 '25 11:02 rishabkapadia

Plus above, Downgrading prettier to 3.4.2 also works on node >=22.12.0 and latest prettier-plugin-blade.

$ node -v
v23.6.1

$ cat package.json
{
  "devDependencies": {
    "@shufo/prettier-plugin-blade": "^1.15.3",
    "prettier": "3.4.2"
  }
}

$ ./node_modules/.bin/prettier test.blade.php
@extends('frontend.layouts.app')
@section('title') foo
@endsection
@section('content')
    <section id="content">
        <div class="container mod-users-pd-h">
            <div class="pf-user-header">
                <div></div>
                <p>@lang('users.index')</p>
            </div>
            <div class="pf-users-branch">
                <ul class="pf-users-branch__list">
                    @foreach ($users as $user)
                        <li>
                            <img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow">
                            {{ link_to_route('frontend.users.user.show', $users['name'], $users['_id']) }}
                        </li>
                    @endforeach
                </ul>
                <div class="pf-users-branch__btn">
                    @can('create', App\Models\User::class)
                        {!! link_to_route('frontend.users.user.create', __('users.create'), [1, 2, 3], ['class' => 'btn']) !!}
                    @endcan
                </div>
            </div>
        </div>
    </section>
@endsection
@section('footer')
@stop

shufo avatar Feb 18 '25 12:02 shufo

🙌 after downgrading prettier to 3.4.2, it works on node 22.11.0, 22.12.0, 22.14.0, 23.8.0

rishabkapadia avatar Feb 18 '25 12:02 rishabkapadia

It seems this prettier's change introduced in 3.5.0 affecting this issue. https://prettier.io/blog/2025/02/09/3.5.0#use-esm-entrypoint-for-requireesm-16958-by-tats-u

related issue: https://github.com/prettier/plugin-php/issues/2418

shufo avatar Feb 19 '25 09:02 shufo

For now, i use 3.4.2 and wait for new fixing,

utqinadhif avatar Feb 19 '25 10:02 utqinadhif

Hello, I'm from Prettier core team, this should be a Prettier issue, and I'm considering reverting the change mentioned in https://github.com/shufo/prettier-plugin-blade/issues/311#issuecomment-2668029476

@shufo Can you transfer the issue to Prettier (Not sure if it's possible), so we can continue the discussion there.

Before Prettier fixes this issue, I'll invgate if this is a different case than https://github.com/prettier/prettier/issues/17139

fisker avatar Feb 19 '25 17:02 fisker

@fisker Thank you fisker! Transfering issue requires both repository write access so I added you to this repository's collabolators. You can transfer this issue if you needed.

I don't know what the root cause is now but also I'll investigate it.

shufo avatar Feb 20 '25 08:02 shufo

Can't transfer

You can only transfer issues between repositories owned by the same user or organization account.

https://docs.github.com/en/issues/tracking-your-work-with-issues/administering-issues/transferring-an-issue-to-another-repository

Never mind.

fisker avatar Feb 20 '25 08:02 fisker

Added reproduction https://github.com/fisker/prettier-plugin-blade-311, and remove module-sync condition from Prettier fixes the issue as expected.

fisker avatar Feb 20 '25 10:02 fisker

@joyeecheung I just want to let you know that your solution from https://github.com/prettier/prettier/issues/17139#issuecomment-2665966391 (replace Promise.all()) won't fix this case. You may want update error message for https://github.com/nodejs/node/pull/57126.

fisker avatar Feb 20 '25 10:02 fisker

I think this may be a different one, the other one is a race when require and dynamic import happens at the same time, and the fix would be making the loading deterministic, which the Node.js is adding a new error message for (replacing "Cannot read properties of undefined"). This one is probably a real cycle, as the existing error message suggests. Either the cycle needs to be broken/made lazy, or there is another suggestion possible, but it is still a different path.

joyeecheung avatar Feb 20 '25 10:02 joyeecheung

Should fixed in Prettier v3.5.2 https://github.com/prettier/prettier/releases/tag/3.5.2

fisker avatar Feb 22 '25 04:02 fisker

FYI I think I figured out what is causing this - fix in https://github.com/nodejs/node/pull/57187 which locally make https://github.com/fisker/prettier-plugin-blade-311 produce

[error] No parser could be inferred for file "/Users/joyee/projects/prettier-plugin-blade-311/fixture.blade.php".

Which is the same as what I see from older Node.js releases without require(esm) e.g. v21

joyeecheung avatar Feb 23 '25 23:02 joyeecheung

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Apr 25 '25 02:04 github-actions[bot]