Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass arguments to “node” executable when running “yarn run”

node can be started with various options. Especially interesting is the --inspect flag:

node --inspect node_modules/.bin/jest some.spec.js

Is it possible to pass the --inspect flag somehow to yarn run? For example:

yarn run test --inspect some.spec.js 

There is a similar question for npm run, where it seems to be not possible.

like image 624
dre-hh Avatar asked Oct 26 '17 11:10

dre-hh


People also ask

How do you pass arguments to a yarn script?

yarn run [script] [<args>] You can pass additional arguments to your script by passing them after the script name. Running this command will execute jest -o --watch . [script] can also be any locally installed executable that is inside node_modules/.

What is Node_options?

The NODE_OPTIONS --max-old-space-size environment variable allows to increase Node's max heap size. Setting an environmental variable allows Node to read this value from your environment and so we don't need to pass this value as an argument every time we run Node command.


1 Answers

In general, yarn run ... does not support passing arbitrary arguments to NodeJS. However, the --inspect flag is an exception depending on your version of Yarn.

As of March 2022, recent versions of Yarn support both --inspect and --inspect-brk arguments for the yarn run command. The answer to your questions is now "yes", and the following will work:

yarn run --inspect some.spec.js

For older versions of Yarn (or even NPM), there are a couple of options.

First, you can use the NODE_OPTIONS environment variable to pass arguments to NodeJS. For example,

export NODE_OPTIONS="--inspect"
yarn run test some.spec.js

then in the package.json, you can define a script to take advantage of this:

"scripts": {
  "test": "jest",
  "test:inspect": "NODE_OPTIONS='--inspect' yarn run test"
}

Second, as you mentioned, you can use NodeJS directly,

node --inspect ./node_modules/jest-cli/bin/jest.js some.spec.js

For older versions of Yarn, those may be your only two options. However, both options work for both NPM and Yarn.

like image 156
timrs2998 Avatar answered Sep 30 '22 19:09

timrs2998