Describe the bug
In order to enable "type": "module" is seen some reports that i should upgrade to latest Berry version. yarn set version berry seems does not work because i was getting (node:1563141) ExperimentalWarning: The ESM module loader is experimental. +"<long minified output> error all the time.
To Reproduce
Create empty directory and check version:
mkdir yarn && cd yarn
yarn --version
1.23.0-20200205.1242
As told in documentation, Yarn 1 is accessible globally.
Check other parties:
node --version
v13.9.0
npm --version
6.13.7
Seems latest or at least one of the latest versions.
Now let's enable nightly:
yarn init -y
yarn set version nightly
yarn --version
1.23.0-20200205.1242
As we see i get no any effect. Still Yarn 1.
BTW - .yarnrc#yarn-path seems to be fixed now and have relative path yarn-path ".yarn/releases/yarn-nightly.js" :)
Let's clean up and enable Berry:
rm -rf .yarn/ && rm .yarnrc.yml
yarn set version berry
yarn --version
2.0.0-rc.29
This time it works like expected (i hope).
Now let's add "type": "module" in package.json and check yarn version again. In theory because i have Node 13 it should work.:
And we get an error again:
(node:1588502) ExperimentalWarning: The ESM module loader is experimental. file:///home/dzintars/Code/yarn/.yarn/releases/yarn-berry.js:2
<long-minified-output-of module.exports=function(e){var t={};..../>
ReferenceError: module is not defined
at file:///home/dzintars/Code/yarn/.yarn/releases/yarn-berry.js:2:1
at ModuleJob.run (internal/modules/esm/module_job.js:110:37)
at async Loader.import (internal/modules/esm/loader.js:167:24)
I would be very grateful for any hints of mistakes i made.
Environment if relevant (please complete the following information):
Same result with Node 14
Setting "type": "module" in your package.json tells node that all .js files are ESM files, but the yarn binary is a CommonJS file.
To unblock your repository, you can rename .yarn/releases/yarn-berry.js to .yarn/releases/yarn-berry.cjs and change the yarnPath property in the .yarnrc.yml to the .cjs extension.
For a more structural solution: both yarn v1 and berry will need to be updated to take this into account. We're going to see more and more ESM workspaces, even though PnP doesn't support it yet.
The easiest solution would be to always use the .cjs extension for yarn binaries. This ensures yarn is correctly loaded for both CommonJS and ESM workspaces and it should work for any version of node supported by yarn v1 or berry (forking to a .cjs file works fine in node 4)
The .cjs workaround as suggested by @bgotink and @aduh95 (in #757) seems to unblock the repo for now:
$EDITOR .yarnrc.yml
# Edit .yarnrc.yml in your repo and set the contents to this:
# yarnPath: ".yarn/releases/yarn-berry.cjs"
#
# The change should only be adding a "c" before the "js" extension. Then:
mv .yarn/releases/yarn-berry.js .yarn/releases/yarn-berry.cjs
Most helpful comment
Setting
"type": "module"in your package.json tells node that all.jsfiles are ESM files, but the yarn binary is a CommonJS file.To unblock your repository, you can rename
.yarn/releases/yarn-berry.jsto.yarn/releases/yarn-berry.cjsand change theyarnPathproperty in the.yarnrc.ymlto the.cjsextension.For a more structural solution: both yarn v1 and berry will need to be updated to take this into account. We're going to see more and more ESM workspaces, even though PnP doesn't support it yet.
The easiest solution would be to always use the
.cjsextension for yarn binaries. This ensures yarn is correctly loaded for both CommonJS and ESM workspaces and it should work for any version of node supported by yarn v1 or berry (forking to a.cjsfile works fine in node 4)