node.js config npm - NODE_CONFIG_ENV - node.js

I am trying to use the config package (npm install config) to be able to use different configurations for different enviromenets.
I am running it on windows 10.
I got 4 files under the config folder : default.json, development.json, production.json and qa.json.
when i am running SET NODE_ENV=production for example it applys to it
but config still aint taking the info from the right file.
var config = require('config');
var port = config.get('appPort');
I done some reading and i found out about another value - NODE_CONFIG_ENV.
I done some testing with :
console.log('NODE_CONFIG_ENV: ' + config.util.getEnv('NODE_CONFIG_ENV'));
console.log('NODE_ENV: ' + config.util.getEnv('NODE_ENV'));
And it seems that NODE_CONFIG_ENV is responsible for the problem because it seems that config is using it instead to decide which file to choose.
My question is how can i make config use NODE_ENV again?
Or if it is not possible how can i set NODE_CONFIG_ENV instead?

Here is a partial solution,
"scripts": {
"dev": "SET NODE_CONFIG_ENV=development&&SET NODE_ENV=development&& nodemon server.js",
"qa": "SET NODE__CONFIG_ENV=qa&&SET NODE_ENV=qa&& node server.js",
"prod": "SET NODE_CONFIG_ENV=production&&SET NODE_ENV=production&& node server.js",
}
I added NODE_ENV in case it returns back to using it but overall I still didnt figure out what caused it to use NODE_CONFIG_ENV instead of NODE_ENV.
Edit: I found the reason! It was because of another npm package called cross-env which i used earlier.

Related

Node.js applies the changes only after restart

I am very new to server side scripting. And I am using NodeJS. My Problem is that after adding some new features to the app, i.e. after changing the code, these changes will be applied only after restarting the server. Till then NodeJS behaves so as though I hadn't changed anything. So for instance if I add console.log("works") and don't restart the server, then it hasn't any effect.
I am using Nuxt.js, which is actually the Vue.js framework but with additional and very usefull features mainly for server side rendering. I didn't integrate the express.js at the beginning of the project, beacause it wasn't planned to write any server side code. So I am normally exporting express and using it, which is pretty fine for me, since I need just a couple lines of code to use the NodeJS file system.
So, as it is pretty hard to code, if I should restart the server once I changed anything, I want to ask you if there is any solution to this problem.
Use nodemon
step 1 : npm install -g nodemon <- this will install nodemon globaly in your system
step 2 : change your start script within package.json
"scripts": {
"start": "nodemon fileName" <- like this //filename is you root file which starts the app like app.js
}
step 3 : npm start
This is already build in into nuxt. You just need to run it in dev mode, not in production.
E.g. for dev with change monitoring
nuxt
For production without monitoring
nuxt start
So in this particular case the following changes to the "scripts" in package.json have solved my problem.
"scripts": {
"dev": "nodemon --watch api --exec \"nuxt\"",
"start": "nodemon nuxt",
}
The following link could also be usefull to you.
Install nodemmon in your application to allow live update npm -g install nodemon
and add the following codes inside your packages json file :
"main": "app.js",
"scripts": {
"start": "node app"
},
on your command line, just type : start

Target certain environments based on console input

To preface the question I am using an ejected create-react-app for the layout of my project.
I have 5 environments that my application is going to be deployed to. Each environment has the same set of services (mostly), for example one might look like:
//environment 1
https://environment1.service1.foo.bar
https://environment1.service2.foo.bar
//environment 2
https://environment2.service1.foo.bar
https://environment2.service2.foo.bar
To achieve this on past projects (Angular/Gulp) I had a gulp task that essentially would look for a variable being passed in
gulp build --environment environment1
The code to do so looks like this:
gulp.task('environment', ['clean-environment'], function() {
log('Copying environment');
var environmentFile = config.environmentSrcDir + 'env2.js';
if (args.environment !== 'env2' ||
args.environment === 'env3' ||
args.environment === 'env4' ||
args.environment === 'evn5') {
environmentFile = config.environmentSrcDir + args.environment + '.js';
}
return gulp
.src(environmentFile)
.pipe(rename(config.environmentService))
.pipe(gulp.dest(config.root));
});
And basically point to the correct file with the correct endpoints inside of it as well as other pertinent variables associated with that environment.
My question is, where given the fact that I am using create-react-app as a starting point, so webpack, and node scripts, how would I accomplish something like this. Basically I want to be able to say yarn build env1 and then the project to set a constant or file of constants as the 'active` constants so to speak.
If you are using "Create-react-app" then you have the ability to define development environment variable through different .env files.
Link: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-development-environment-variables-in-env
.env:
REACT_APP_GOOGLE_CLIENT_ID = XXX-YYY-ZZZ.apps.googleusercontent.com
REACT_APP_API_PROTOCOL = http:
REACT_APP_API_HOST = localhost:3000
NODE_PATH = src/scripts
PORT = 9001
.env.production:
REACT_APP_GOOGLE_CLIENT_ID = ZZZ-YYY-XXX.apps.googleusercontent.com
REACT_APP_API_PROTOCOL = https:
REACT_APP_API_HOST = api.my-applicaton.com
NODE_PATH = src/scripts
Read different .env configs according to current command (start / test / build). dev.env for start and test. prod.env for build. If custom config does not exist — read env variables from .env file.
Blockquote
You will tell which .env file will be used with your start project command.
You should have something like this in your package.json file under scripts object:
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
"build": "npm run build-css && react-scripts build",
Then you can start your project using specified commands. From the documentation, files on the left have more priority than files on the right:
npm start: .env.development.local, .env.development, .env.local, .env
npm run build: .env.production.local, .env.production, .env.local, .env
npm test: .env.test.local, .env.test, .env (note .env.local is missing)
For example, if you start with npm run build, you will be able to access variables defined in .env.production file.
In JavaScript code, you can use process.env.REACT_APP_API_HOST.
Also, see this link on medium.com: https://medium.com/#tuchk4/why-i-love-create-react-app-e63b1be689a3

How to set NODE_PATH=. for node.js app hosted in openshift

I use custom node.js cartridge on openshift
icflorescu/openshift-cartridge-nodejs .
How can i set NODE_PATH=. for app start in package.json ? Should i provide it in package.json like that : "start": "NODE_PATH=. NODE_ENV=production node app.js" ,
either i should use something like here
Dindaleon/hapi-react-starter-kit - some npm package like cross-env
I have line in main app.js file. There is folder named 'config', in the same directory with app.js, in folder config placed file index.js, file index.js have code with 'module.exports = Object.assign({ ...some conifg object... });' . When i delete NODE_PATH=. , node throws "Error: Cannot find module 'config' " .
var config = require('config');
I'm the author of openshift-cartridge-nodejs :-)
Having "start": "node app.js" in your package.json should be just enough.
If you take a look at bin/install, you'll see that NODE_ENV is already set to production by default in the cartridge setup script.
Also, I'm not sure what you're trying to achieve by setting NODE_PATH to .. There's a single Node.js version installed.
If you're generally interested in how you can set custom environment variables in an OpenShift-deployed application, have a look at the docs here. Basically you'll have to use the rhc command-line utility like this:
$ rhc env set <Variable>=<Value> <Variable2>=<Value2> -a App_Name

How to access environment variables from `npm start`?

I have set up an environment variable (on OSX):
export npm_package_config_test_user=user42
and a javascript file:
-- index.js --
console.log(process.env.npm_package_config_test_user);
which if I run like node index.js provides me with the expected answer. now, with this definition in the package.json:
"scripts": {
"start": "node index.js"
},
I can do: npm start but when I do my environment variable value comes back as undefined.
so 2 questions:
1) why? and
2) what do I need to set up so I can pick up the value correctly?
thanks
so what I found is that npm clears the npm_package_* space before use, so I set the variable and it wipes it out when it runs. however, this works:
export test_user=user42

How to stop nodemon from changing port on each restart of express app?

I am new to nodejs and this is first time I am using nodemon. I am using nodejs on windows. I have got following in my package.json file
"scripts": {
"start": "nodemon ./bin/www"
}
And I use npm start from command line to start my express app. The process start with a default port which is annoying. But what is even more annoying is that every time I change a file nodemon restarts the application, sometimes on an entirely different random port number. I tried changing the script section in package.json file to the below but that did not make any difference
"scripts": {
"start": "nodemon ./bin/www 3000"
},
From the comments it seems you're specifying the port through an env variable, let's call it EXPRESS_PORT. The node process doesn't inherit it when you start it with npm because npm start creates a new shell with its own environment. So you end up passing port undefined to express. That makes it bind to a random free port. To fix this you can set the variable in the start command:
"scripts": {
"start": "EXPRESS_PORT=3000 nodemon ./bin/www"
}
Or you can export it from your shell with export EXPRESS_PORT=3000 and then run npm start. If you do this you need to make sure to always export before starting the server, so you might want to place the export in ~/.profile or ~/.bashrc.

Resources