-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Allow using npm configuration to set NODE_OPTIONS
beyond lifecycle scripts
#8048
Comments
I think the design as-is solves the problems it solves in the correct way. You either want I don't know where you got the idea that ~/D/s/scripts $ cat x.js
console.log('x is loaded')
~/D/s/scripts $ npm pkg get scripts.s
"semver 1.1.1"
~/D/s/scripts $ npm pkg get scripts.f
"npm run s"
~/D/s/scripts $ npm run s --node-options='-r ./x.js'
> [email protected] s
> semver 1.1.1
x is loaded
1.1.1
~/D/s/scripts $ npm pkg get scripts.f
"npm run s"
~/D/s/scripts $ npm run f --node-options='-r ./x.js'
> [email protected] f
> npm run s
x is loaded
> [email protected] s
> semver 1.1.1
x is loaded
1.1.1
If you need the initial instance of npm itself to have node-options, you must set it in the environment beforehand. That's a classic bootstrapping problem there. |
This has been around since at least npm@6 fwiw. This is why we were comfortable w/ node documenting its use with npx. It already reasonably works for most folks using npm today. |
This is exactly the problem we are having. In order to instrument npm itself, we need to ensure that I'm wondering whether a possible solution could be (and I can imagine this being an opt-in) to add a configuration key to enable this behavior (e.g. I'm not hard-set neither on this solution or anything similar, I'm trying to figure out what tool we can use to reliably set preload modules when npm is run. I'm wondering if perhaps npm allows to list modules to be loaded in a way similar to how Node.js does with |
This is already what's happening if you set What part of the original invocation of npm do you need, that you wouldn't need if npm were to re-invoke itself with node-options set? The delta between those two is not a lot of code. |
Any solution that exists in npm itself is going to be adding complexity in places it by all rights shouldn't be. I understand the desire to have this managed invisibly to the user, but unfortunately setting the initial environment that runs when you invoke node is (and should remain) outside the scope of npm itself. I know other tools and languages have scripts that put you into a shell mode where the environment is set as you need. This still seems like the right avenue. This is not complexity we need to add to npm. |
Not too much indeed, although one thing that we need to track are command line arguments, which IIUC would be lost if
Absolutely, I don't want to make npm a generic tool in this regard, but identifying a way in which a module can be loaded by npm itself when starting up. Do you think there's some other way to achieve this? Or alternatively, do you think there's some other way of instrumenting npm? |
As of npm 11, it's possible to use the
node-options
configuration key to specify the content ofNODE_OPTIONS
for lifecycle scripts (i.e. it wouldn't be picked up, for example, bynpm test ...
-- but it would be fromnpm pretest ...
).I'm wondering whether it would be feasible thinking about the idea of allowing users to opt-in into using this configuration key to set
NODE_OPTIONS
for npm commands as well.I'm not hard-set on this solution, I'll share some context around why I'm making this proposal.
Context
As a software engineer at Gradle, I'm part of a team which is adding support for npm to our build observability product, Develocity.
We created a module that can be preloaded (
NODE_OPTIONS="-r ..."
) and, when enabled, collects events from the run and sends them to the Develocity server.We realized that setting
NODE_OPTIONS
can have ergonomics issues: setting it globally means that the module cannot be found for projects that don't have the package installed (and would be picked up by any Node.js process), and unfortunately usingnode-options
from the npm config would not have an effect when running, for examplenpm test
.I'm open to discuss other solutions that would fit well with our use case of instrumenting npm.
The text was updated successfully, but these errors were encountered: