How to I use env variables in npm scripts - node.js

I'm building and signing an Electron app and I'm trying to contain my certificate location and sha pass to an environment variable.
Is this how I pass the env variables to an npm script?
"electron-pack-win": "CSC_LINK=process.env.REACT_APP_CSC_LINK CSC_KEY_PASSWORD=process.env.REACT_APP_CSC_KEY_PASSWORD build --win
Worth mentioning that I'm using create-react-app

Based on the comments,
Your .env already contains REACT_APP_CSC_LINK.
All you need to do is define a variable in the .env
For example,
REACT_APP_CSC_LINK=some_value
CSC_LINK=some_value
Electron will be able to access the environment variables using process.env.CSC_LINK

The ambient environment variables will be passed through to the script/program being run by npm, so if you set something in your shell - on Windows,
set CSC_KEY_PASSWORD=hello
or in Bash shells,
export CSC_KEY_PASSWORD=hello
, they will be available to the program being run.
Only if you want to override some values would you use the syntax you mentioned (or cross-env to be cross-platform compatible).

Related

build the application, and an environment variable error occurs when accessing after build

I am using Nuxt3 and Prisma to build the application, in the development environment, they run fine, when I execute the npm run buildcommand, there is also no output error.nuxt3 outputs the .output folder.
build
But when I use the command start to run, prisma prompts me environment variable could not be found.
error
I recreated the .env file under the output .prisma folder, but it doesn't work.
recreated
How can I solve the environment variable in production problem?
Your .env file should be in the same directory as your schema.prisma file. I'm not sure what framework/library you are using, but it looks like it emits a schema.prisma file within node_modules, which is not where your environment variables should go. Wherever you actually have your schema.prisma is where the .env file should go.
Take a look at the Prisma documentation about environment variables if you need help configuring where it's supposed to go: https://www.prisma.io/docs/guides/development-environment/environment-variables

Replace env variables in the .npmrc with dotenv before `npm install`

I have to work with some packages in the private registry. So, in my package.json in the dependencies section I have a lines like this one:
...
"dependencies": {
"#myco/my-awesome-package": "^0.4.5",
...
}
...
There is authentication required for the private registry, so I have to create the .npmrc file in my project:
registry=https://registry.npmjs.org/
#myco:registry=https://myco-registry-path/
//myco-registry-path/:username=${MYCO_REGISTRY_USER}
//myco-registry-path/:_password=${MYCO_REGISTRY_PASSWORD_BASE64}
Yes, I know about _authToken, but in my case it is easier to use user and password.
Anyway, here you can see two env variables: ${MYCO_REGISTRY_USER} and ${MYCO_REGISTRY_PASSWORD_BASE64} which I have to replace before npm install.
I know the very simple solution for this problem: put them to the "global" env variables for example to my .bash_profile (or any terminal profile of your choice).
But I do not want to keep variables like this in the "global" scope because the are important only for the current project. What I want to do is to use dotenv. I want to create a .env file in the root of my project:
MYCO_REGISTRY_USER=myco-registry-username-value
MYCO_REGISTRY_PASSWORD_BASE64=myco-registry-password-value-base64
I want that this values replace env variables in my .npmrc on the install action. But when I try npm install I get an error: Error: Failed to replace env in config: ${MYCO_REGISTRY_USER}. I can understand why it happens. Possibly because npm reads .npmrc values first and try to replace env variables and fails, because in this moment it know nothing about dotenv.
My question is how to deal with it?
Short summary:
I do not want to keep env variables in the terminal profile, instead I want to put it in the .env file inside my project.
I have to replace env variables in the .npmrc file with dotenv before npm install
I know this answer might come too late, but in case anyone else is looking for answers, here's a solution:
You need to prepend your scripts with dotenv-cli as so:
dotenv npm install
or in my case where the file was not .env:
dotenv -e .env.local npm install
The problem is that you cannot save this anywhere so that someone can use it with "npm install" somehow. Definitely npm preinstall is run after reading .npmrc so it fails too.
You will need to either document it well or just include a small shell script, but if you're supporting different OSs then it can get funny really fast...
Happily so, CD platforms like Netlify allow you to set environment variables manually.
But I guess this must not be the nicest of starts if someone clones your repo and the first they've got is a failing npm install 🤷‍♂️
Also, check this one out: locking-the-vault-on-font-awesome-npm-tokens

How to access process.env variable from a script command

i have an angular project. I want to build the project to be different depending on whether the process.env.NODE_ENV file is test or production.
Angular has a ng build command that you can tag with configuration to define whether the project should be built as "test" or "production". Instead of hard coding these values i want it to be based on the process.env variable.
How do i access this process.env.NODE_ENV variable within my package.json script command.
npm run build
"build": "ng build --configuration=process.env.NODE_ENV",
At the moment i get the following error
Configuration 'NODE_ENV' could not be found in project 'demo'. Error:
Configuration 'NODE_ENV' could not be found in project 'demo'.
NODE_ENV=prod npm run build
This will set the environment variable in process.env so you can access anywhere in node.js script.
This way you can pass any value into NODE_ENV while running.
ng build --configuration=$NODE_ENV
or
ng build --configuration=%NODE_ENV%
depending on your platform.
process.env is only accessible through a javascript file running on node.js, what you need here is to use the shell syntax

NodeJS: add options to npmrc

I am currently running my script as
node --no-deprecation main.js
I was wondering if there is a way to add this option to ~/.npmrc or otherwise (environment variable), such that I do not need to add the option to the command line. I would like to just run my script as node main.js and to be shown no deprecation warnings.
To make this question more general, is there a general way in NodeJS of setting flag values in a config file rather than adding it to the command (say I am interested in running with --trace-warnings as well.
I am looking for something cleaner than say alias node="node --no-deprecation".
I already tried adding this line inside ~/.npmrc
no-deprecationss=true
Reference:
https://nodejs.org/dist/latest-v7.x/docs/api/cli.html#cli_no_deprecation
If you're looking for a global configuration, setting the --no-deprecation flag in the NODE_OPTIONS environment variable in your .bashrc or other shell config file is what you're looking for, as long as you're using node v8 or above.
https://nodejs.org/docs/latest-v10.x/api/cli.html#cli_node_options_options
You can do it via package.json. e.g
"scripts": {
"main": "node --no-deprecation main.js"
},
and run it via
npm run main
or
yarn main
I know this is an old question, but for anyone new who comes across it, one reproducible way to do this without having to add it to every npm script and without affecting other projects (which is what would happen if you added it to .bashrc or .profile), is to add a .npmrc file in your project with node-options set in it.
It accepts the same flags as NODE_OPTIONS except applied to every npm script in your project without affecting others and keeping your scripts DRY.
e.g.
# Sets the flags in NODE_OPTIONS when running `npm run myScript`
node-options='--no-deprecation'

create-react-app local and dev environment variables

I have .env.local with
REACT_APP_BACKEND_BASEURL=http://localhost:8080/
and .env.development with
REACT_APP_BACKEND_BASEURL=http://deployedserverurl:8080/
how do I select the correct env file on start?
As it is now, it prefers the dev env file over the local.
npm start --env=local doesnt seem to work, am I missing something?
Environment variables are imported depending on the current environment. There is a built in special environment variable with create-react-app called NODE_ENV. In summary, when you run npm start the NODE_ENV variable is set to development and set to production when running a npm run build. Therefore, if you create a .env.development in the root of your project, when you run npm start these variable definitions will be searched for in the environment.
Also, ensure you're using them correctly by using process.env.REACT_APP_APP_BACKEND_BASEURL.
If you need more information regarding all of the details about the different type of .env files, check these out from the React Docs:
env: Default.
.env.local: Local overrides. This file is loaded for all environments except test.
.env.development, .env.test, .env.production: Environment-specific settings.
.env.development.local, .env.test.local, .env.production.local: Local overrides of environment-specific settings.

Resources