Dotenv not loading environment variables (Ubuntu, Yarn, Mocha) - javascript

The issue
I'm working on an application that uses the dotenv package.
It's not currently setting any of the environment variables when I run yarn test or yarn start I'm getting errors because the environment variables are not being set.
App.js has this line near the top of the file
require('dotenv').config(); - there are no references to process.env before this line of code is called.
Things i've tried
Install dotenv globally.
Completely delete the node_modules folder and rerun yarn install
Providing the environment variables manually (this actually worked, but I've confirmed with another developer that the tests and the app is running just fine on his system without having to provide the environment variables manually.
App.js (entry point)
7 require('dotenv').config();
.env (entry point)
LOG_LEVEL=testlevel
APP_NAME=testapp
TestController.js (consumer)
const log = logger(process.env.LOG_LEVEL, process.env.APP_NAME, 'TestController');
I'll take any help I can get at this point.

Ok, figured it out. Apparently the server was actually being started by mocha and there was a reference in there to process.env that was the problem. After adding the require('dotenv').config() to the beginning of the server file, the problem was resolved.
Thanks guys!

Related

Env variables are undefined on frontend with React

I'm trying to move all the sensitive variables to .env file. It works just fine server side on Node but I'm having trouble doing it with React.
I referenced these answers: this and that
1.I changed my .env so it looks like this
REACT_APP_SIGNER_PK = xxxxxx-xxxxx-xxxxxx
Added the variable itself to the file containing script
const REACT_APP_SIGNER_PK = process.env.REACT_APP_SIGNER_PK;
Placed .env inside of my root folder
I restart the server after the changes with yarn run dev
To test out the changes I put this print statement from the file where I put the env variable in
console.log(REACT_APP_SIGNER_PK);
On the restart of the local server I check the console and in the terminal where the server is running I see the print statement with my environmental variable, but in the browser console I see this print as undefined
The only thing that differs in my action from the references is that I use yarn run other than npm start
What did I do wrong?
Thanks to the comment from #OFRBG I managed to find a solution.
Adding REACT_APP_ app to the my .env variables did not work because I was using nextJS instead of Create-React-App.
In order to fix the problem this problem for nextJS users add NEXT_PUBLIC_ instead as a prefix for your .env variables

Hi, Can't figure out why heroku can't find mongoose module. Any idea?

I Can't figure out why heroku can't find mongoose module
It's because mongoose is set as a devDependency. Heroku usually deploys things in production mode, which means development dependencies are not installed. You should set mongoose as a regular dependency in your package.json.
(You could also tell Heroku to run in development mode, but that's probably not the right solution since you almost certainly want mongoose to be available in production.)
It's because mongoose is listed on devDependencies of your package.json.
By default Heroku will strip out the modules declared under devDependencies before deploying the application.
One way to solve this is move mongoose to dependencies on package.json. Other way is set NODE_ENV environment variable to other value than production that is the default, so Heroku will mantain the modules on devDependencies.

configure environments with restify

I'm familiar with gulp and the ability to have a distinct configuration for each environment within a single configuration file. That way running gulp dev would allow you to start your server with the configuration of your dev environment, gulp staging for your staging and gulp prod fro production.
I'm now looking at restify and am trying to determine if something similar can be done with it. I've tried researching this online and haven't found anything meaningful. Is this possible and if so could somebody provide an example?
You can use dotenv package to load different configuration file. For example
.env.dev For Development environment
.env.prod for Production environment
.env.test for Testing environment
you can import file according to NODE_ENV var
or you can simply add all configuration variable in one file for example
.conf.env and import it.

How do I use dotEnv when there is no .env file?

I have a project that locally has a .env file. When I set up the environment variables on my local machine I use:
import dotEnv from "dotenv";
dotEnv.config();
It find the .env and loads up all the variables. When I push the project to Heroku, I don't push up the .env file. I expected dotEnv to understand that, but I get this error:
{ Error: ENOENT: no such file or directory, open '.env'
How do I get around that I am not pushing up a .env file? What is the proper way to use .dotEnv?
I'm a contributor to the dotenv module. You can safely ignore this error. It is returned for your convenience in case something isn't working as expected.
I don't much like the solution proposed here: https://github.com/motdotla/dotenv/issues/126 It amounts to conditionally requiring /dotenv depending on an environment variable being set. This seems brittle and like extra work to me.
The neatest solution for this I've found doesn't pollute my production code with any references to dotenv at all. Just include dotenv in your dev dependencies and run your app locally using the node require switch:
$ node -r dotenv/config ./lib/index.js
This is the same way you might load babel-register.
Now your code doesn't care about where the env variables come from. Works with nodemon, etc. too. You can even specify an alternate env file:
$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars
It's all on the npm page for the package.

LoopBack installation through npm not working on Windows [duplicate]

When I run npm install -g <package> it installs the packages in my user/AppData/Roaming/npm/npm_modules/ folder. This sub-folder is not in my PATH so if I try to run the package without explicitly calling the entire path the call fails with a '<package>' is not recognized as an internal or external command, operable program or batch file.
What can I do to fix this?
Thanks
I'm using win8.1 and I found that the nodejs installer didn't add the path to global node modules to the system PATH. Just add %AppData%\npm; to the user variable(since %AppData% dir is depending on user) PATH to fix it.
You will need to log out then log back in for the change to your PATH variable to take effect.
SET PATH=%AppData%\npm;%PATH%
You have to run this line SET PATH=pathtonodejs;%PATH% (where pathtonodejs is where you installed nodejs) once the installation for nodejs is complete and it should work.
It turned the problem was a change in behavior of the module I was using.
I'd been following old tutorials for using Express.js. The old tutorials assumed Express would be in my path after installing it globally, but as of Express v4.0 there's a separate Express module you have to install in order to get it in your path

Categories