Describe the bug
I have tried yarn plug and play with node 13+.
When running node --require=./.pnp.cjs index.js there is an error saying Error [ERR_REQUIRE_ESM]: Must use import to load ES Module.
Running npm install && node index.js works as expected.
To Reproduce
git clone https://github.com/dmail/yarn-pnp.gitcd yarn-pnpnode --require=./.pnp.cjs index.jsScreenshots


Environment if relevant (please complete the following information):
Additional context
Please note how I had to put an empty package.json inside .yarn/releases/ so that .yarn/releases/yarn-berry.js works.
Also your documentation asks to run yarn set version berry but it fails saying Invalid version descriptor "berry".
.
Am I doing something wrong ? Is it expected that plug and play is not yet compatible with node 13+ ?
According to https://github.com/yarnpkg/berry/issues/1150#issuecomment-607889290 it seems expected. PnP is not yet compatible with es modules.
Could you explain the reason for requiring .pnp.js/.pnp.cjs be added to node process at runtime? .pnp.js isn't meant to act as a module even though it's now written as a module.exports. It provides an optimized way to map deps across projects that v1 did not in any way come close to accomplishing. In fact, if you log out the node process object during the pnp lifecycle you can already see .pnp.js is in fact already included as a runtime requirement.
I don't understand what you want to know.
To my understanding plug and play exists to avoid creating the whole node_modules immediately. Instead they are dynamically unzipped from an archive. To achieve this yarn override require to unzip a file when it's required.
Knowing this I think it's mandatory to execute pnp.js that will override node require mecanism before actually running any other code. Right?
I think I understand what youâre asking, but please let me know if not. Youâd like to enable specific, native features within the context of the active node process at runtime that are not enabled already by default (i.e, passing flags as arguments to node like ââexperimental-modulesâ, etc.). To further clarify, you are not asking how to require/run external modules at runtime (i.e. passing âârequire esm ârequire dotenv/configâ to ânodeâ so as to not have to manually enable these features by including them first before anything else in your app runs). Is that correct? If it is, fortunately the solution is made very simple particularly with ânode v13.xâ. Provide the runtime features youâd like enabled as a string to the environment variable âNODE_OPTIONSâ. Run your code using âyarn node /path/to/script.jsâ as recommended in the docs. When run, you will then see the features enabled. Yes, using PlugâNâPlay does result in the contents of ânode_modulesâ from being included within a project in the traditional way they used to; where each module and any dependencies they have are directly downloaded and included as directories within ânode_modulesâ as paths that ânodeâ then is able to resolve natively. But thatâs the bi-product of PlugâNâPlay, not why it exists. It exists because the traditional way ânodeâ module resolves are handled is not super performant, results in heavily bloated project size, and is limited in any approach by which a developer could share compartmentalized code throughout the scope of a project (i.e. what workspaces are all about). PlugâNâPlay abstracts module resolution away from ânodeâ completely, providing as an alternative instead a subset of extendable features via custom fetchers, resolvers, etc. all available with the context provided via âYarn v2â. All that said, the â.pnp.jsâ file is generated automatically during yarnâs execution lifecycle. It isnât an actual module, itâs a map which describes how/when/what/where/why all the packages within your project, their dependencies, and their dependencies, all the way down the line need to look like for ânodeâ to then properly resolve and thus making them ârequirableâ. If you want to include external modules directly to ânodeâ at runtime as project dependencies, not just devDependencies, and have them configured and wired up ready to run at the start of your project initialization, thereâs a way to go about doing that as well. Rather than explain it all out here, check out the @yarnPkg repo itself to see how to do this. In short, itâs doable by changing the â.yarnrc.ymlâ variable âyarnPathâ value to a custom path which offers a script file that essentially acts as a go between for yarn to be run from. Thatâs one way. Another would be to follow the instructions provided in the âpnpapiâ docs. This approach ieffects how the final â.pnp.jsâ file is ultimately generated in a way that seems to be what would be considered âbest practiceâ. Hope this helps. Cheers!
I want to run a JavaScript file having a dependency where both are written import/export syntax.
And I want to use node 13+ because it support package written using import/export.
index.js
import { value } from "whatever";
console.log(value);
deps/whatever/index.js
export const value = "hello world";
Check https://github.com/dmail/yarn-pnp to see the whole file structure.
After running yarn install in this context whatever package is inside a zip.
node index.js would throw module not found.node --require=./.pnp.cjs index.js throw must use import to load ES Moduleyarn node index.js throw must use import to load ES Module too
@dmail and @larixer
I've created a repo based on the demo of the issue you mentioned. The README.md is purposefully over-verbose for anyone else who might run into this in the future. The actual solution is really very simple. Check out the repo here:
https://github.com/DaneTheory/yarn-pnp-with-esm
Let me know if you've got any further questions. I'd be happy to create a plugin that handles automating everything if that's something the community is doing here. The solution doesn't exactly fit into what the yarn-compat plugin is doing under the hood. Cheers!
I like having that sort of demo repository it helps a lot, thank you very much.
If I understand correctly the final command looks like this
yarn node --require esm ./file.js
And does the following:
yarn node: Injects yarn plug and play module resolution into node (using pnp.js).node --require esm: Injects esm conversion into node.yarn node but esm seems aware of yarn pnp as shown by hook/pnp.js../file.js: Execute file.js. Before actually executing file.js, it is first converted to CommonJS format by esm thanks to node --require esm. Also require will be handled by yarn thanks to yarn nodeI am attracted by yarn plug and play especially zero install philosophy. I am ready to use yarn node path/to/file.js to run code and benefit from it.
However relying on esm conversion disturbs me a bit. Also having to use CommonJS format. I have to test it on actual projects and see how it goes.
That would be great to see pnp.js hook into node experimental loader. I can understand yarn wants to wait for loader to stabilize first.
Hi! đ
This issue looks stale, and doesn't feature the reproducible label - which implies that you didn't provide a working reproduction using Sherlock. As a result, it'll be closed in a few days unless a maintainer explicitly vouches for it or you edit your first post to include a formal reproduction (you can use the playground for that).
Note that we require Sherlock reproductions for long-lived issues (rather than standalone git repositories or similar) because we're a small team. Sherlock gives us the ability to check which bugs are still affecting the master branch at any given point, and decreases the amount of code we need to run on our own machines (thus leading to faster bug resolution faster). It helps us help you! đ
If you absolutely cannot reproduce a bug on Sherlock (for example because it's a Windows-only issue), a maintainer will have to manually add the upholded label.
Tracked in #638.