How to access process.env variable from a script command - node.js

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

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

How to configure IDE to run any node.js file from explorer?

I have a node.js project with some tests (it's my own test framework).
The structure of the project is the following:
app.js
tests
- test1.js
- test2.js
Inside the app.js file, I have a special setting that allows me to specify the path for running tests. If I run the following command from the terminal:
`node app.js` all tests from the tests folder will be run
If I run the following command from the terminal :
`node app.js ./tests/test1.js` - only the specific file will be run
I want to run these tests from IDE (I need to run all tests or the specific file) - from the Visual Studio Code or IntelliJ IDEA (by click on the file).
I've figured out how I can run all tests:
for IntelliJ IDEA I set up Node.js configuration and now I can run tests by click on the app.js file:
in Visual Studio Code, I can run all tests from the app.js file from IDE by clicking on Run command on the left panel
My question is: how can I run the specific file by click on it from IDE (clicks on test1.js/test2.js)? I want to click on the file with the test and imitate the same command as I run it from the terminal:
node app.js tests/test1.js
I know that if I'd used some test framework like Mocha I've could use the special plugins, but using an external test framework is not an option for me.
I figured out, that the best option for my task is would be to set up an NPM configuration for the project. The window for setting up the NPM configuration has some fields which should be filled:
package.json - path to your package.json
command - run
scripts - the name of the main script from the package.json file
environment - your environment variables (if you have it - they should be filled, because otherwise, the npm runner could not catch them from the '.env' file)
The most important field for my task is 'Arguments' - I fill it with the path to file that I want to run and then in my node.js app I'm able to access it via the proccess.argv array.

What are the tasks performed by "npm start" command in React?

Please help with my below questions:
"npm" is the one which comes when we install Node.js. Am I correct?
"create-react-app" package installs/loads the Babel & Webpack which are required for our React project. Am I correct?
When will the React code gets compiled & Translated? Does the below points are correct?
a. After creating React project and developing some code, we are loading our application in browser by running the "npm start" command. So while running this command, will the Babel is going to compile the React code and convert it to JavaScript code which has ES5 standards? Does this conversion to ES5 happens when we run the "npm start" command?
b. Also I learned that Webpack is going to merge all various files within the React project to a single .js file. So does this merging of all the different files by Webpack will be performed when we run "npm start" command?
So if my above understanding is correct, the React code will be Compiled, Translated & Merged into a single file when we run "npm start" command. Correct?
npm start is really just a command that exists in the package.json which can be configured to do whatever you please. It is typically use to kick off all things needed to "start" the app. In the context of a react app created using create-react-app the start command will call react-scripts start and that points to a file called react-scripts which sits in your node_modules/.bin. If you want to see everything that happens you can read through that file.
In short tho, you are correct that it will use babel to transpile the code to something the browser understands, it will use webpoack to create one bundle file (or multiple if you are using code splitting). It will also start a webpack dev server which will usually listen on port 3000 and it will open your default browser to your app. These default settings can be overwritten in the package.json.
Hope this gives you some clarity.

Read process.env at runtime webpack

I'm building my function with webpack that get deployed to AWS lambda.
I currently have to specify the environment variable at compile time for them to be available as per webpack doc.
This is awesome for web deployment, but for node I might want to change the values passed at build, by changing the env file i'm sourcing from. For example .env.staging or .env.production without having to rebuild the application or directly on my lambda environment.
How can I achieve this if possible? I'm using webpack4

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