yarnpkg / berry

📦🐈 Active development trunk for Yarn ⚒
https://yarnpkg.com
BSD 2-Clause "Simplified" License
7.34k stars 1.1k forks source link

[Bug?]: Cannot find module 'esbuild' imported from ../ #5241

Open dalisoft opened 1 year ago

dalisoft commented 1 year ago

Self-service

Describe the bug

pnpm-esbuild/frontend on  master via ⬢ v19.5.0 took 2s 
❯ node -r ./.pnp.cjs ./scripts/build.mjs
node:internal/process/esm_loader:78
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'esbuild' imported from /Users/dalisoft/Desktop/bug/pnpm-esbuild/frontend/scripts/build.mjs
Did you mean to import esbuild/frontend/.yarn/unplugged/esbuild-npm-0.17.5-ba581df653/node_modules/esbuild/lib/main.js?
    at new NodeError (node:internal/errors:399:5)
    at packageResolve (node:internal/modules/esm/resolve:794:9)
    at moduleResolve (node:internal/modules/esm/resolve:843:20)
    at defaultResolve (node:internal/modules/esm/resolve:1058:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:837:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:418:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v19.5.0
pnpm-esbuild/frontend on  master via ⬢ v19.5.0 
❯ yarn why esbuild
└─ frontend@workspace:.
   └─ esbuild@npm:0.17.5 (via npm:^0.17.5)

To reproduce

git clone git@github.com:dalisoft/pnpm-esbuild.git
cd pnpm-esbuild
cd frontend
yarn set version stable
yarn
node -r ./.pnp.cjs ./scripts/build.mjs // or yarn node scripts/build.mjs

Environment

System:
    OS: macOS 12.6.3
    CPU: (8) arm64 Apple M1
Binaries:
    Node: 19.5.0 - /private/var/folders/kj/92wfmyss40g5lzxfmy9czwwh0000gn/T/xfs-12b8fbd2/node
    Yarn: 3.4.1 - /private/var/folders/kj/92wfmyss40g5lzxfmy9czwwh0000gn/T/xfs-12b8fbd2/yarn
    npm: 9.3.1 - /opt/homebrew/bin/npm

Additional context

No response

merceyz commented 1 year ago

Seems we've forgotten to update the documentation; when using ESM you need to register the loader as well.

node -r ./.pnp.cjs --loader ./.pnp.loader.mjs
dalisoft commented 1 year ago

@merceyz There no file .pnp.loader.mjs, where i can get this file? Thank you

merceyz commented 1 year ago
yarn config set pnpEnableEsmLoader true

https://yarnpkg.com/configuration/yarnrc#pnpEnableEsmLoader

dalisoft commented 1 year ago

thank you, oh, sorry, yep, i able to fix by two way:

Using yarn node SCRIPT works much stable than node -r

merceyz commented 1 year ago

I'll keep this open since we need to update the documentation.

dalisoft commented 1 year ago

@merceyz https://github.com/yarnpkg/berry/issues/5241#issuecomment-1414390053 did not help. Only adding {type: module} helped

merceyz commented 1 year ago

You also need to run an install after setting that config in your project.

dalisoft commented 1 year ago

Yes, i know and still not worked as expected. But finally solution I'm did find, that's has been my goal. Thank you for help and useful links

mondo192 commented 1 year ago

I also encountered this bug. Running the below commands was the only thing that worked for me. YMMV.

yarn config set pnpEnableEsmLoader true
yarn install