13

I am a bit ashamed that I cannot find a good solution to this myself, but alas.

Using npm we generally have a package.json file with a scripts segement for storing the tasks we want our toolchain to execute routinely, e.g.:

"scripts": {
    "build": "webpack --env production",
    "start": "webpack-dev-server"
}

Now we might have updated parts of our toolchain - in this example webpack - and suddenly we get a message like this one:

(node:10868) [DEP_WEBPACK_DEPRECATION_ARRAY_TO_SET] DeprecationWarning: Compilation.modules was changed from Array to Set (using Array method 'reduce' is deprecated)
(Use `node --trace-deprecation ...` to show where the warning was created)

As-is, it's completely useless for diagnosing and resolving the issue. Something somewhere in some build script is wrong. ... Yay!

In the case of webpack and its plugins at least, we can obtain pretty useful trace output invoking on the CLI:

node --trace-deprecation node_modules/webpack/bin/webpack.js --env production

Quite a cumbersome line, is it not? There is a whole slew of reasons why we really do want to avoid this. In no particular order:

  • deprecations in particular are a single uncoloured line in a sea of colour coded messages - easily missed
  • we have to build the project again after noticing the deprecation
  • in the script we simply use the name of our desired build tool, here we have to provide the full relative path
  • we have to copy the desired parameters from our package.json to the CLI (if e.g. different environments activate different plugins - not a general use case and thence easily overlooked even when a programmer already got the idea to handle deprections)
    • due to the added .js we cannot just copy the whole script body either
  • sheer, unadulterated laziness

So...

  • Is there a way to activate --trace-deprecation in the scripts portion of package.json?
  • If not, is there a way using alternative build script sources, e.g. via nps?

... I guess, I could always put the node invocation into a separate batch file, but I'd much rather use a single place to manage all the build scripts in.

Intended use case:

I'd like this option to be always on when building locally and probably on select CI branches, so I can output the full trace right into the log and mark the step as unstable. Having a dedicated npm script "build:trace" or similar would seem to be the straightforward solution - if it can be done.

Mokubai
  • 95,412
Zsar
  • 537

1 Answers1

13

Yes indeed—you can pass many node CLI flags as environment variables, which can be inlined in a scripts command. For example:

"build": "NODE_OPTIONS='--trace-deprecation' webpack",

For docs on which flags are supported, see: https://nodejs.org/api/cli.html#node_optionsoptions

Also, just prepending the environment variable like that won't work on Windows. If you want cross-platform env vars in your scripts fields, the best solution I'm aware of is cross-env.