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
Related
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
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'
I am using 'create-react-app' npm from React starter https://github.com/Microsoft/TypeScript-React-Starter,
How do I have different config files for different environments (e.g. Dev, CI, production)? I want to store the endpoints, some app settings in those config files.
The only way that I can think of is to eject the project (npm run eject) and create the config json file using web pack.
Is there any better way to handle the different config files with 'create-react-app' npm? Thanks.
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.
I have a ecosystem.json file for PM2 that handles apps, deployment, and environment variables. very similar to the example scripts on the deployment documentation page.
I'm curious about best practices involving storing this file in version control and on the server's file system; seeing as it contains things like password and keys.
I have considered ignoring it from version control, however doing that means the deployment PM2 process won't work. Deploying without the ecosystem file in the repository means the post deploy script won't find it as it wasn't cloned to the machine.
npm install && pm2 startOrRestart ecosystem.json --env production
This means I am now using a private repository and checking in the ecosystem file. Is it possible to have a public repository and still use pm2 deployment?
Finally what are the best practices about having the ecosystem file on the file system? It seems that PM2 requires that it be there.
My understanding of env variables was that only the running processes know about them. Is this a misunderstanding? Is a file containing sensitive env variables okay to be left in the application code directory?
Should I add another line to the post-deploy script to delete the ecosystem file after running the application? Would that cause problems with things like restarting the processes or anything else?
npm install && pm2 startOrRestart ecosystem.json --env production && rm ecosystem.json