Home > Software engineering >  Quotations being added to combined npm script
Quotations being added to combined npm script

Time:01-10

I'm attempting to combine 2 npm scripts in one, but when I do the output is incorrect and causes flags to not be passed. Using the dotenv package is not an option and using ampersands is not helping.

So in package.json I have this -

"define:local": "cross-env NODE_ENV=local FOO=bar BAZ=foo",
"local:dev": "npm run define:local tsnd --inspect --respawn path/to/mycode.ts",

When I run npm run local the output in the terminal is cross-env NODE_ENV=local FOO=bar BAZ=foo "tsnd" "path/to/mycode.ts" which means it is dropping the flags being passed to tsnd and causing local development issues. It's also putting quotes around the tsnd command and file path for some reason.

If, however local:dev is run without "define:local" (e.g. tsnd --inspect --respawn path/to/mycode.ts the output is tsnd --inspect --respawn path/to/mycode.ts without quotes as expected and flags are included.

Furthermore, If I try to use ampersands in the command to combine the 2, the cross-env command no longer works and the environment variables are not defined.

How can I combine these 2 scripts and keep the flags and environment variables in tact?

CodePudding user response:

You need -- to tell npm to pass the args to the command, instead of passing them to npm run:

"define:local": "cross-env NODE_ENV=local FOO=bar BAZ=foo",
"local:dev": "npm run define:local -- tsnd --inspect --respawn path/to/mycode.ts",
  • Related