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.
Related
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
I have a nodejs project which is using nodemon for providing environment variables when running locally. I have a need to have the developers easily switch between two different nodemon config files (two different sets of environment variables). But I can't see a way to specify a config file for nodemon. How can I accomplish this.
You can specify the config file using the --config flag:
https://github.com/remy/nodemon#config-files
I have a Gatsby project with a couple functions. I'd like to migrate some of my variables to env variables. Setting these up in Netlify is a breeze.
However, locally I'm trying to test my functions using netlify dev. It starts up fine, but as soon as I hit my endpoint, there are issues because I'm accessing non-existent properties on process.env. I have tried adding a .env and .env.development file in the root of my project (where I run netlify dev) but it doesn't seem to be reading them. The documentation I've ready only mentions netlify.toml files, but the whole point is to not have it in source control.
Is there something I need to do for the command to pick up my .env file?
Configure your build to grab your .env and copy it over to the location that is used to deploy the website.
An example would be to add the copy step to your build command:
// netlify.toml
[build]
command = "do-build && cp .env functions/"
Although this will only work for local deploys with the netlify command. If you want to do it with their automated deploys, there is no way around checking in the .env file.
To avoid that, you are expected to set those values through the project UI in Netlify's website.
You can create environment variables in the toml file or in our UI. You might use the latter to keep sensitive values out of your repository.
https://docs.netlify.com/configure-builds/file-based-configuration/#inject-environment-variable-values
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
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).