-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
esm: add import.meta.env #55202
esm: add import.meta.env #55202
Conversation
Review requested:
|
I'm very confused here - environment variables simply aren't supposed to be module-specific, since they're for the entire environment. |
To add on to what @ljharb said, I also don't see a use-case. If a user wants to access the environment (in ESM or CJS), they have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only to land this as an experimental feature, I think it could be worth experimenting with, getting wider feedback, and then considering more carefully from there.
That is - landing this should not be considered an endorsement of it unflagging, but rather an endorsement to exploring the cross-platform environment variable design space.
One thing I'd like to consider for a final API is mutability. E.g. is this a "live" object? Should it be frozen at some point in time? Can you modify it to set environment variables? But I wanted to start with the most basic version that matches my understanding of the existing versions in other tools. (And, of course, there's the base question of should this be how env access works in generic code.) |
Landing it flagged and experimental to iterate is always fine ofc! If this lands unflagged tho, then people will start depending on it, "experimental" or no. |
What's the benefit for having yet another way of doing things? |
IIRC the consensus is we should not add things to Th echo what others have said, it would make little sense for that proposal to come from Node.js when there's already another way to get env variable (and indeed env variable are not really module specific). The mutability aspect is certainly worth discussing, could you elaborate about the use case(s) you have in mind? (maybe we should take that discussion to a separate issue) |
703c545
to
058cea7
Compare
Sent a revised wintercg PR here: wintercg/import-meta-registry#7
The reason why I'm interested in this addition is, funnily enough, the part that's not about environment variables. I'm looking at the equivalent of the There's definitely also other use cases around environmental settings that may only be visible to specific modules but that's something for a more in-depth discussion, e.g. in the context of wintercg maybe? Having a placeholder in nodejs means that some prototype/demo building could happen in nodejs. But it's definitely not a blocker since bun has shipped this already. |
058cea7
to
a7b0093
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, this can land behind a flag. I would recommend @jkrems to restart the WinterCG discussions on this.
|
The difference is that it's a boolean and always set when the |
a7b0093
to
d593f7e
Compare
d593f7e
to
c4edd0e
Compare
Putting my 2¢ out there:
|
c4edd0e
to
fe3801b
Compare
That's a valid philosophy and there's absolutely ways to abuse such a feature. At the same time, it's a common practice to strip debug code during builds and I don't think it's possible to just ignore the constraints that lead to that practice. Code shouldn't depend on the environment it's running - but not at any (size/performance) cost. I agree that this feature requires more discussion (and hopefully changes to the API shape & semantics) before it could be unflagged. This implementation is meant for experimental use only. I personally don't believe we should add this to node in the end if the semantics really are "just expose env vars". |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #55202 +/- ##
==========================================
+ Coverage 88.39% 88.41% +0.01%
==========================================
Files 652 652
Lines 186565 186596 +31
Branches 36046 36055 +9
==========================================
+ Hits 164916 164975 +59
+ Misses 14908 14888 -20
+ Partials 6741 6733 -8
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think providing better access to NODE_ENV should really not be a goal. Frankly, NODE_ENV was a mistake and caused many problems down the line.
That sounds like a very surprising way to paraphrase the stated goals. But there is a kernel of truth to it, of course. There is indeed overlap with NODE_ENV even if the actual stated goal is to reduce use of NODE_ENV by providing a viable alternative to some of the use cases. In the end, it's fair to say that this doesn't need to exist in node. People who want this are likely to already use a build tool. And they already have a solution that they use today: apply that build tool to their code before node sees it. So as long as the final solution errs on the side of "not dev by default" (which I sure I hope it will, given the hard lessons from NODE_ENV's mistakes), it's as easy as saying "if you want to enable debug assertions in node, use a build tool." With that in mind, I'll close the PR. Thanks for the feedback! |
Ever since Vite introduced it,
import.meta.env
has been spreading to other tools - and to user code. This leads to some disruptions: E.g. running code that's built using Vite in nodejs-based unit tests is tough because of this missing API.The
import.meta.env
API has been added to Vite, bun, and rspack at the very least so far.