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

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

Related

Module not found: Error: Can't resolve 'fs' in dotenv/lib

All of a sudden, when creating a react production build, I get this error.
> safe-courier#0.1.0 build
> react-scripts build
Creating an optimized production build...
Failed to compile.
Module not found: Error: Can't resolve 'fs' in '/workspace/safe-courier/client/node_modules/dotenv/lib'
I have searched on the web, found similar cases but different frameworks of which all were not of help in regards to this issue.
I have tried to uninstall dotenv and reinstall it again but i get the same error. I'm not sure what could be the problem understanding that fs module is part of nodejs and comes bundled with it
To solve this:
Create the .env file at the root level of you app
Name your variables beginning with REACT_APP_ // that's the key !!
Restart your server with npm start each time you change an env variable
Use your variable with process.env.NAMEOFYOURVARIABLE
No need of dotenv for your React app.
I solved the same problem;
npm install dotenv-webpack --save-dev
Create .env file under root folder
create env variable as
REACT_APP_YOURVARIABLE=itsvalue
Create webpack.config.js file under the root folder with following content
const Dotenv = require('dotenv-webpack');
module.exports = {
plugins: [
new Dotenv()
]
}
Then wherever you want to use variable in .env write
process.env.REACT_APP_YOURVARIABLE
There is no need to import dotenv. It is already done in webpack.config.js
1- As already mentioned by Stéphane Sulikowski, No need to use dot-env in react projects
Why?
"dot-env" uses some modules that are only supported by nodejs but not supported by "browser engines" like fs, os etc. React-code-bundle runs in the browser and the browser doesn't support module "fs", so if any modules reference the "fs" module will get the same error.
There is some inbuilt support by reactjs to use environment variables stored in a .env file and begins with REACT_APP_
2- If you have to use it for some reason use "env-cmd"
npm install env-cmd
3- create environment specific .env files like .env.local OR .env
4- In your "environment specific" OR .env file, add variables beginning with REACT_APP_
REACT_APP_API_BASE_URL="http://localhost:4000"
5- Use these environment variables in your code files
like console.warn (process.env.REACT_APP_API_BASE_URL)
6- OPTIONALLY...... configure package.json something like this
...
"scripts": {
"start": "env-cmd -f .env.local react-scripts start",
"build:staging": "env-cmd -f .env.staging react-scripts build",
"build:production": "env-cmd -f .env.production react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Note - When you add a new variable in .env .... files, you have to run npm start OR related...
Just like Reno mentioned just create your .env at the root with the name prepended with REACT_APP and it will work out of the box
Example .env file
REACT_APP_GITHUB_API_URL=https://api.github.com/graphql
Usage:
process.env.REACT_APP_GITHUB_API_URL
If you are using create-react-app this article describes the behavior of environment variables: https://create-react-app.dev/docs/adding-custom-environment-variables/
Note that the document frequently reminds you of this warning:
WARNING: Do not store any secrets (such as private API keys) in your React app!
Environment variables are embedded into the build, meaning anyone can view them by inspecting your app's files.
To securely use secrets such as passwords and tokens, consider setting up a server that can deliver this data to the frontend via HTTP. For example, once users authenticate into your app you send their credentials to a microservice that can validate their identity and return an API key or session to be used for subsequent REST API calls.

React environment variable not working in dev

Please put me out of my misery. I see scores of other people had the same issue and I don't see a solution.
I am trying to put my sensitive keys into environment-specific files (.env.development, .env.staging, etc). The keys work fine if I put them in .env but I need this file for some other items which must be pushed up to source control. All of the files are in root (I see that this was a common mistake). Is there something with webpack that is the issue? I have restarted the server instance every time I make a change.
const express = require('express');
const path = require('path');
const app = express();
require('dotenv').config();
console.log('ENV', process.env.NODE_ENV); // this returns "development"
console.log('Hello?', process.env.REACT_APP_HELLO); // this returns "undefined"
As noted I am surfacing the environment correctly.
"start": "SET NODE_ENV=development&& node server/index.js",
from package.json
REACT_APP_HELLO=BLAH
from .env.development
I assume you're in cmd.exe because of the set. Add a space before the &&: "start": "set NODE_ENV=development && node server", (no need to specify index.js. On non-Windows systems this would be NODE_ENV=development node server.
EDIT:
To get .env.development working, change the dotenv line to this: require('dotenv').config({ path: '.env.' + process.env.NODE_ENV }). (source), or the custom-env package: require('custom-env').env(process.env.NODE_ENV). Neither of those inherit from the regular .env though, so if you need that, check out dotenv-flow. I haven't tried the last package, but it seems to have the most features and the least amount of config to get working.

node.js config npm - NODE_CONFIG_ENV

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.

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

Node & Elastic Beanstalk: Set environment NODE_ENV=prod

I need to execute the following command for production environment:
NODE_ENV=production node app.js
I tried passing it as a command under Configueration:
I get the following error in the logs:
sh: NODE_ENV=prod node app.js: command not found
I also tried:
NODE_ENV=prod //
error: sh: NODE_ENV=prod: command not found
NODE_ENV=prod app.js //
error: sh: NODE_ENV=prod app.js: command not found
What's the best way to execute the following command when launching the app on ELB:
NODE_ENV=production node app.js
You don't need to set the node command manually. Elastic Beanstalk attempts to start app.js, then server.js, and then npm start in that order. You can set the value of NODE_ENV in the "Environment Properties" section under "Configuration".
As others have mentioned you can manually add them by going to Configuration -> Software -> Environment properties.
A second way to do this is to add a .ebextensions/environment.config file.
Add the directory .ebextensions at the root of your project.
Create the file environment.config within .ebextensions.
Add your environment configurations.
Example of environment.config:
option_settings:
- option_name: NODE_ENV
value: production
You need to use the "Environment Properties" Not the node command.
Key: NODE_ENV
Value: production
I would just set NODE_ENV in Environment Properties under the same configuration area. You can also set this in your .ebextensions config.

Resources