process.env is undefined after using .env file in node.js - javascript

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

Related

Why my .env variables are not working in Parcel?

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.

Require is Undefined

I am trying to write a web application to work with with Tumblr api. I have already used npm and installed both globally and in the file itself: browesrify, tumblr and tumblr.js. whenever I upload the file to my server which runs on Netlify I get the error that require is undefined.
/* global $ document console len alert require */
// Authenticate via OAuth
var tumblr = require('tumblr');
Here is my error:
script.js:5 Uncaught ReferenceError: require is not defined
I am aware of the fact that this question has been asked before and I have gone through all instances of these types of questions and none of the solutions have worked for me so perhaps something has been updated in one or more of the frameworks.
Netlify will not have Browserify installed on a global scope in npm, so you should set it up in your local project. You set it up using the global install locally, so it works on your local environment.
Make sure you have your package.json setup correctly and it exists.
Install browserify as a development dependency.
$ npm install browserify --save-dev
Add a build script command to your package.json
"scripts": {
"build": "browserify main.js -o bundle.js"
},
Note Your command should use the correct browserify command.
Use npm run build as your Netlify build command.

How to create new environments with create-react-app?

I am using react-scripts v2 (beta) and I have read the documentation here.
We need to create many environments.
We want to store env file under folder config/env.
We might use javascript file in config/env/staging.js because .env seems to be only for root directory.
We have real environment :
default
development
staging
preproduction
production
We expect the default environment to be a default config under version control in config/env/default.js, it must be the default configuration used when doing npm start
We expect the user to be able to override with a file with no version control. (something like config/env/default.local.js
Basically that can be reduced to two issues :
Is it possible to relocate env location folder?
How can we create and select a new environment on npm start/build?
without ejecting.
Just copy the environment file to .env before starting / building. You can even put .env in .gitignore that way
"start": "cp $ENV .env && react-scripts start"
Then run it:
ENV=config/staging/.env npm start
There are lots of ways of doing what you want without ejecting, since it's all preprocessing (before your app starts / builds).

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.

Node.js - Globally installed module showing 'MODULE_NOT_FOUND'

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');

Categories