Adding Environment Variables to the node process using CMD - node.js

I am trying to add the environment variables to the node process on start using CMD. I want achieve this without using the script in package.json & not using the dotenv package
I'm going through one of the node.js lectures & found the below command which will set the environment variables to the node process as below :
NODE_ENV=development nodemon server.js
After some research I found that it is different for windows & as below :
SET NODE_ENV=development&&nodemon server.js
I tried the same in the cmd but got the error message NODE_ENV is not recognized as an internal or external command.
Just wanted to know how in CMD this adding environment variables works

Related

PATH variables empty in electron?

I'm trying to access items in my PATH environment variable from my electron instance, when I run it with npm start while developing it through node.js I get all the expected variables, but when I run the electron application with my resources inside I'm left with only usr/bin
This is how it looks like when I run it from npm:
And this is how it looks when run from the electron mac application precompiled:
Does anyone know why this could be the case? And if I can do anything to reach my normal PATH variables
UPDATE:
After a lot of research I found out that GUI applications ran from finder or docker in Mac OSX use different environment variables compared to if they are ran from the terminal:
This can be edited through plist files, either globally or application specific
You can use fix-path package. Works perfectly!
const fixPath = require('fix-path');
console.log(process.env.PATH);
//=> '/usr/bin'
fixPath();
console.log(process.env.PATH);
//=> '/usr/local/bin:/usr/bin...'

View .env variables of Node.js process

I use package dotenv to set env variables in .env file. Here is the content of .env:
NODE_ENV=development
PORT=4000
Then, I run nodemon --exec babel-node -- src/index.js to start the project.
How to check value of env variables by command line?
I don't want to use console.log in the JS source code, I want something like so (in terminal):
node
> process.env
But it doesn't show the values I have set in .env file.
Could you give me some resolution?
From your console, type following for mac/ubuntu:
echo $NODE_ENV
and following for windows:
echo %NODE_ENV%
If you want to avoid the use of dotenv as sometimes it creates confusion that where to call the dotenv. So, you can use foreman node module and can start your project by command nf start. It will automatically load .env variables into your project without calling dotenv.

How to run Node controlled Phantomjs/Casperjs script in Heroku

I've written a Casperjs script to do some scraping, however, after running into memory exhaustion issues, I've now written a node script to turn Phantom off and on via exec. I can run this with no issues locally, however when I deploy to heroku I get the following error
Error: Command failed: casperjs turnitoffandon.js
ERROR: stderr was:/bin/sh 1: casperjs: not found
I used the nodejs buildpack and have Phantom and Casper defined in my dependencies. In the heroku bash, running phantomjs --version returns 2.1.1 and casperjs --version returns 1.1.4.
Do I need to define where Casper is? If so how? I have set my PATH variable as /usr/local/bin:/usr/bin:/bin:/app/vendor/phantomjs/bin:/app/vendor/casperjs/bin:/node_module/casperjs/bin , like in this SO question
The issue actually had nothing to do with Heroku. As noted in this SO answer, using exec and providing any environmental variables in the options parameter replaces the entire set of environment variables. This includes the path, effectively overwriting any paths already specified to Heroku in the buildpack and npm modules.
You can create a copy of process.env and pass that along in the parameters in addition to other environmental parameters needed to be passed.

How to use environment variables to run node app in atom IDE

I have node project that contains .bat file to set all the env variables & then it run my server.js .
How can i achieve the same using IDE?
Currently when i am trying to run the code from atom ide its giving error for those environment variables not set as they are required to run my project.
While running from command prompt i can simply run the .bat file which sets all the environment variables required for my project followed by "node server.js" command to run my project.
But from atom IDE when i run server.js it is not able to find the environment variables. How can i define/set these environment variables so that the atom IDE can read it & use them when i run server.js.??
Install XAtom NodeJS Debugger: apm install xatom-debug-nodejs
Toolbar will be shown,
Choose your project,
click on Node.js button and in the popup shown configure your project setting and
environment variables.

Why can't I run my node.js express web application

Node.js and the express generator are really handy and simple to understand. I cannot however get my server to start by running c:\my-application-root>DEBUG=my-application ./bin/www
Windows doesn’t seem to understand that command. I can however run it by calling node ./bin/www
Am i missing something?
Did you try set DEBUG=my-application followed by node ./bin/www? Windows does not allow setting an environment variable in the way that Linux and others do.
1st command 'set DEBUG=my-application'.2nd command 'npm start'
First you need to go the cmd that is supported by node (search for node cmd when you click in windows icon if you're using windows 7)
type
set DEBUG=my-application
Second
simply cd c:\my-application\bin\
Then type
node www
www is the file that contains the script needed by node to run your server, DEBUG if set as an environment variable will also help you run node against it since the path will be known

Resources