Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

node.js - Pass the same parameters to node for multiple scripts in package.json

In my package.json, I have several scripts that call node with the same parameters over and over. To simplify that invocation, I've created a package.json variable, and am invoking the scripts like this:

{
  "nodeParams": "--experimental-specifier-resolution=node --harmony -r source-map-support/register",
  "scripts": {
    "dothis": "node $npm_package_nodeParams myscript.js",
    "dothat": "node $npm_package_nodeParams another-script.js",
    ...
  }
}

This is quite common for calling compiled TypeScript code, and worked fine in NPM v6.

However, it no longer works in NPM v7 due to a breaking change:

RFC 21 Environment no longer includes npm_package_* fields

What is the currently recommended solution, or some alternative?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sure, you just need to use npm_package_config_*

From the link you provide (https://github.com/npm/rfcs/blob/latest/implemented/0021-reduce-lifecycle-script-environment.md)

Each key in the config object will be included, but npm will not override values with a <pkgname>:<keyname> config value if one exists.

e.g.

{
  "config" : { "nodeParams": "--experimental-specifier-resolution=node --harmony -r source-map-support/register" },
  "scripts": {
    "dothis": "node $npm_package_config_nodeParams myscript.js",
    "dothat": "node $npm_package_config_nodeParams another-script.js",
    ...
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...