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.
Related
I'm trying to use env variables in Parcel parcel-bundler version 1.12.4 but it returns undefined. Here is my folder structure. As you can see the .env file is in the same folder as the package.json file.
This is the variable I'm using API_KEY=myapikey and I'm trying to console log it from my index.js file like this console.log(process.env.API_KEY).
I just want to know if I'm doing something wrong or if need to open an issue.
Thanks in advance!
Edit: I fixed it by deleting the .cache folder.
You have to install npm i dotenv package
Add the following command to index.js file:
require('dotenv').config()
this imports your credentials from .env package and now you can access and use it in your project.
I have created an application using node.js & protractor. the following start script is used in the package.json file for running my application
"start": "protractor conf/conf.js"
I have created a .env file and add one sample variable TestManaf='1234556'
But it shows error like 'the value is undefined'.
After this, I have directly added TestManaf='1234556' in the start script.
"start": "set TestManaf='1234556' && protractor conf/conf.js"
It is working fine. Why .env file not supported? is any additional configuration required?
You need to install the package dotenv
use the following command:
npm install dotenv
First thing you should do in your file is load the dotenv env vars, put this line as soon as possible in your import hierarchy.
require('dotenv').config()
You can see more information about using this package in the documentation
My Nuxt project uses system environment variables to set client ids, secrets, urls, etc...
An example is in my nuxt.config.js where I set several properties with the following formula:
{
something: process.env.SOMETHING || 'something_for_dev'
}
Nuxt dev version is working fine because looks after the process.env.SOMETHING and correctly use something_for_dev.
Nuxt on staging has its own configuration on Azure and the SOMETHING env var is correctly set but suddenly it still continue using something_for_dev...
What should I do to let Nuxt use the sys env vars I set on my Server rather than the default used for dev? Thanks
Env variables are set build time, not runtime. So it will be the env variables that set during your build, which seems you do on your dev machine.
So you can either build with proper env variables or use nuxt-env module, which allows runtime variables, but keep in mind that it wont allow webpack to optimize dead code and environment variables used in nuxt-env are exposed client side, so if you store secrets use the secret config option
In addition to Aldarund comment above you could build a proper env variables in nuxt by following:
Use cross-env: npm install cross-env
In your project add a folder named environment
and under the folder you could have different environment (e.g. development, staging, production)
Your environment configs will have your baseUrl and other configs, let's say for development you could have your localhost then for the staging or production you could have your API_URL
defaults.json \\development
defaults.prod.json \\production
In nuxt.config.ts build > extend configure your different environment
This will replace defaults.json depending on which environment we will run our script in package.json.
In package.json configure your script to what environment will be run (e.g npm run start will use NODE_ENV=development which will use the defaults.json with baseUrl: http://localhost:3000 and npm run build will use defaults.prod.json with baseUrl: http://www.API_URL.com and other configs
For more detail you could see cross-env
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!
I am using Node.js for a project. I installed WebdriverIO globally using
npm install -g webdriverio
In one of my files, I have:
file.js
var webdriverio = require('webdriverio');
When this file gets loaded, I get an error in the console that says:
Message:
Cannot find module 'webdriverio'
Details:
code: MODULE_NOT_FOUND
If I comment out the line var webdriverio = ..., my code runs fine.
Considering I've installed webdriverio globally, I do not understand why I'm getting this problem.
When you install globally, you should then go to the root of your app and call:
npm link webdriverio
P.S. no need to call npm install, since you will end up having two separate installations of this module, one in global and another in your local node_modules folder
Node.js require looks into the local node_modules folder.
Check this link to learn how to load modules from the global modules folder:
https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
If the NODE_PATH environment variable is set to a colon-delimited list
of absolute paths, then node will search those paths for modules if
they are not found elsewhere. (Note: On Windows, NODE_PATH is
delimited by semicolons instead of colons.)
You need it locally for your app, run npm install webdriverio in the root directory of your app.
Node looks for modules in the innode_modules folders only (starting with current folder and then looking in the folder above). In order to make it work, you have to install this package locally as well.
npm install webdriverio
You can use the full global path:
const wdio = require('/usr/local/lib/node_modules/webdriverio');