I'm trying to run an npm package (Apollo Rover) via an npm script. The command on the package itself requires an Access Token, and I'm not comfortable having that directly in the package.json so wanted to pull it out and contain it in a .env that won't be commited.
I've tried using cross-env and cross-env-shell, but my tests aren't working.
.env
ACCESS_TOKEN="jdaksc8ds7vp98vwy8"
package.json
{
"scripts": {
"test": "cross-env-shell \"echo Hello $ACCESS_TOKEN\""
}
}
Output
% npm run test
> [email protected] test
> cross-env-shell "echo Hello $ACCESS_TOKEN"
Hello
Is there something I'm missing to provide this functionality? I also need this to run on *nix based systems and Windows.
CodePudding user response:
You need to evaluate the .env
properly
{
"scripts": {
"test": "cross-env-shell \"echo Hello $(grep '^NODE_ENV' .env)\""
}
}
CodePudding user response:
cross-env is used to set environment variables inline when running node commands. In your case you have defined your variables in a .env
file which is different.
When populating environment variables from .env
files you'll need to use dotenv or similar.
In your specific case I can recommend using env-cmd with the -f
option as the simpler option.
{
"scripts": {
"test": "env-cmd -f ./custom/path/.env node \"echo Hello $ACCESS_TOKEN\""
}
}