-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[Bug] Yarn plug and play does not work with ESM #1149
Comments
According to #1150 (comment) it seems expected. PnP is not yet compatible with es modules. |
Could you explain the reason for requiring |
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. 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
|
@dmail and @larixer 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 |
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:
I am attracted by yarn plug and play especially zero install philosophy. I am ready to use That would be great to see |
Hi! 👋 This issue looks stale, and doesn't feature the 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 |
Tracked in #638. |
@DaneTheory
I already spent hours trying to figure out. I haven't tried your suggestion yet. We use dotenv in our build scripts to switch between env files. |
@r1y4h hey, did you figure out how to use an expression like that with Yarn 2? |
Doesn't either of these work for you?
|
the first script using "yarn node" did not work for me:
I used env-cmd instead
|
That tells me nothing, what isn't working? Does it throw?
They aim to solve the same thing but |
This works:
|
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 sayingError [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.git
cd yarn-pnp
node --require=./.pnp.cjs index.js
Screenshots
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 sayingInvalid version descriptor "berry"
..
Am I doing something wrong ? Is it expected that plug and play is not yet compatible with node 13+ ?
The text was updated successfully, but these errors were encountered: