.env for prod and developpment with nodejs - javascript

I saw lot of different ways, some looked normal some others looked a bit more patchworked.
Can we use package json script to chose our env variables ? What is the right way to do it with nodeJS and how to do it ?
I have already made an .env. It contains api keys which are global for dev and prod. But I have some variables, the URL variable for exemple, which won't be the same depending on dev or prod.
Here are my scripts in the package.json
...
"scripts": {
"dev": "nodemon app.js",
"prod": "node app.js"
}

Use cross-env package to define a NODE_ENV for the command you are running. e.g. "prod": "cross-env NODE_ENV=production node app.js"
In the code, read the env file based on the NODE_ENV config. FWIW dotenv package can help with reading .env files.

Related

Restrict React route for the development only

Is there a chance I can restrict the React route, to work only in the development mode, and how to achieve this?
You can use an environment variable for this, for example NODE_ENV.
https://create-react-app.dev/docs/adding-custom-environment-variables/
Personally I use https://www.npmjs.com/package/cross-env so environment variables work the same way on all platforms. Then you can define a start:dev parameter under scripts in your package.json file.
"scripts": {
"start": "cross-env NODE_ENV=production node index.js"
"start:dev": "cross-env NODE_ENV=development node index.js"
}
And then conditionally run code with
if (process.env.NODE_ENV === 'development')

Webpack pass env variables after build

My use case is the following. I make a production build like this:
cross-env API_URL=my_url yarn build
and then run it like this:
yarn start:prod
and it all works ok. But what I want to be able to do is:
yarn build
and then start it like:
cross-env API_URL=my_url yarn start:prod
so that I can test the same build against different backend environments. Is this doable?
Right now, if I try it, the API_URL doesn't get picked up after build and it defaults to the one I have in an .env file (because I support a default case). I'm aware that Webpack needs the env variables during build, but maybe there's a workaround I'm missing.
My build script, inside package.json is:
cross-env NODE_ENV=production env-cmd .env.prod --no-override --config config/webpack.prod.babel.js --color -p --progress --hide-modules --display-optimization-bailout
and my start:prod script is:
cross-env NODE_ENV=production env-cmd .env-prod --no-override node server

NestJs load environment variables on npm start

There`s a more easy way to load your env vars using .env files.
Just add --require dotenv/config to your start script, like: node --require dotenv/config server.js.
https://github.com/motdotla/dotenv
But, the problem is that this does not seems to work with nodemon and I can`t figure out how to do it.
I tried:
"start:dev": "nodemon --require dotenv/config",
Can someone help?
According to this GitHub issue, nodemon does not accept cli parameters for node. However, you can use this workaround to pass params:
nodemon --exec "node -r dotenv/config" index.js
You can put this in your npm start command by editing the package.json:
"start": "ts-node -r tsconfig-paths/register -r dotenv/config src/main.ts",
^^^^^^^^^^^^^^^
If you want to use it in the start:dev command, edit the nodemon.json file:
"exec": "ts-node -r tsconfig-paths/register -r dotenv/config src/main.ts"
^^^^^^^^^^^^^^^^
if someone has reached here looking for a generic answer of how to set environment variables in nestjs then you got to read this official docs Nestjs Configuration

Watch template files and copy them to dist/ folder

I'm using typescript on my project and I can successfully watch + compile .ts files and output them to dist folder.
here is the scripts part of my package.json
"start": "npm run build && npm run watch",
"build": "npm run build-ts && npm run tslint",
"test": "cross-env NODE_ENV=test jest --watch",
"watch": "concurrently -k -p \"[{name}]\" -n \"Typescript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run serve\"",
"serve": "nodemon dist/server.js",
"build-ts": "tsc",
"watch-ts": "tsc -w",
"tslint": "tslint -c tslint.json -p tsconfig.json"
The problem is I want to use js templating engine (nunjucks) and I need to watch the view files inside the views folder and move them to the dist folder.
Is there a way by just using npm scripts or nodejs?
Or do I need to use other tools like gulp or webpack?
I have the "same" request to for a CRUD graphql back-end server, but don't want to use gulp or webpack just to keep it simple.
I see that you use nodemon like me. Then, according the docs at https://github.com/remy/nodemon, it can be used it to monitor changes of any kind of file other than the default js. More over, nodemon can monitor the status of other transactional server other than node.
The first task is detecting the changes of wanted files: in my case I want copy the *.gql files in my src/schema folder to build/schema folder. For that, you can use the ext for the kind of files, and watch option for the source folder to explore.
The second one task is matter of copying the files. Naturally, you can use the copy command of your host OS. In my case I use the DOS xcopy command of the Windows shell (or cp in Unix like OS). nodemon has an "event-hook" with the event option, that can execute a command line when an event occurs. Just we need the restart event of the node server when the changes are detected for nodemon.
You can use the command line options, or a global config file, or in you local package.json project config file. I show up the last one using nodemonConfig section of package.json:
"nodemonConfig": {
"watch": [
"./src/schema",
"./build"
],
"ext": "js,gql",
"events": {
"restart": "xcopy .\\src\\schema\\*.gql .\\build\\schema /Y /O /R /F /I /V /E"
}
}
Ozkr's answer is great, I just want to add what worked for me, I had to change it a bit as nodemon was running into an infinite restart otherwise:
"nodemonConfig": {
"watch": [
"./views",
"./public"
],
"ext": "hjs,js",
"events": {
"restart": "cp -r views dist \n cp -r public dist"
}
}
copy-and-watch does just that:
I use this code to copy html files during development:
"copy_html": "yarn copy-and-watch src/mail_templates/* prod/mail_templates --watch --clean",

setting up node application for production server using babel

I'm trying to get my first ever node app ready for a production server.
build and serve scripts I'm basing on that given by babel
"scripts": {
"start": "nodemon --exec babel-node server.js --ignore public/",
"build": "babel server.js -o server_compiled.js",
"serve": "node server_compiled.js",
"dev": "webpack -wd"
}
npm run build
works as expected
npm run serve
results in an error:
some/path/config.js:3
export default{
^^^^^^
SyntaxError: Unexpected token export
This is from a config file that I'm referencing in server.js for port host and db.
Do I build this file also or what have I done wrong?
Any help appreciated.
So, I eventually figured out I had to compile all of the .js files in the app individually. I'm sure there must be automated way to do this but thought I'd leave the solution in case anyone can't find anything else.

Categories

Resources