Node process.env.VARIABLE_NAME returning undefined - node.js

I'm using environment variables on my mac to store some sensitive credentials, and trying to access them through Node. I added them into my environment profile with
export VARIABLE_NAME=mySensitiveInfo
When I use echo $VARIABLE_NAME I receive the correct output (my sensitive info).
However, when I am trying to access this same variable in Node with process.env.VARIABLE_NAME and try to print it out on the console, I get an undefined.
Other environment variables appear to be okay though. For example, when I console.log(process.env.FACEBOOK_CALLBACK_URL), it prints the correct value to my console. I added FACEBOOK_CALLBACK_URL a few days ago.
Do I have to restart my machine or something? Does it take a certain time before environment variables become available in Node? The closest answer I've seen on SO is this post, but nobody was able to figure out why it was happening.

nodemon.json file is only for setting nodemon specific configuration
So for create custom environment variables we can use dotenv package
First , Install dotenv package
npm install dotenv --save
after that create .env file in root and include environment variables as bellows
MONGO_ATLAS_PW=xxxxx
JWT_KEY=secret
Finally, inside your app.js file insert following after your imports.
require('dotenv').config()
Then you can use environment varibale like this
process.env.MONGO_ATLAS_PW
process.env.JWT_KEY

process.env.VARIABLE_NAME returns undefined because the Node.js execution environment does not know the newly added VARIABLE_NAME yet. To fix the issue, the Node.js execution environment (e.g. IDE) need to restart.
The following steps can be used to reproduce this issue:
Open IDE such as WebStorm and write a simple Node.js program: console.log(process.env.VARIABLE_NAME). It will print undefined as expected, as VARIABLE_NAME is not defined yet. Keep the IDE running, don't close it.
Open environment profile such as .bash_profile and add export VARIABLE_NAME=mySensitiveInfo in it.
Open system console and run source .bash_profile, so that the above export statement will be executed. From now on, whenever system console is opened, VARIABLE_NAME environment variable exists.
In system console, execute the Node.js program in step 1, it will print mySensitiveInfo.
Switch to IDE and execute Node.js program, it will print undefined.
Restart the IDE and execute Node.js program, this time, it will print mySensitiveInfo

I got a problem just now , and I use this solved it in webpack config
const plugins = [
new webpack.NoEmitOnErrorsPlugin(),
// after compile global will defined `process.env` this Object
new webpack.DefinePlugin({
BUILD_AT : Date.now().toString(32),
DEBUG: process.env.NODE_ENV !== 'production',
'process.env': {
'NODE_ENV': JSON.stringify(process.env.NODE_ENV || "development"),
'VARIABLE_NAME': JSON.stringify(process.env.VARIABLE_NAME)
}
})
]

For everyone who might have this issue in the future and none of the solutions above are working, it may also be because you're running the node <filename>.js in a subfolder or subdirectory, and since your .env file is in the root folder, processs.env.<variable> will always return undefined.
A simple way to check is to try the following code 👉
const test = require('dotenv').config()
console.log(test)
In the console we get the following error
{ error: Error: ENOENT: no such file or directory, open 'C:\Users\vxcbv\Desktop\voisascript-auth\model\.env'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at Object.config (C:\Users\vxcbv\Desktop\voisascript-auth\node_modules\dotenv\lib\main.js:72:42)
at Object.<anonymous> (C:\Users\vxcbv\Desktop\voisascript-auth\model\db.js:1:32)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: 'C:\\Users\\vxcbv\\Desktop\\voisascript-auth\\model\\.env' } }
Emphasis on the Error: ENOENT: no such file or directory, open.
To solve this, just navigate back to the root and run the file from there, but specify the full path of the file from the root, like 👉
node /<subfolder>/<file.js>

please check the dir of .env file, If it is in same file as your app.js or (abc.js) than move .env to one level up

Close the code Runner Or Ide environment once and reopen it.
If you use VS code, close it completely. (Or CMD or Power Shell or ...)

I had the same issue. In my case the env file and db.js was inside a subfolder configs.
So, in index/server.js , while importing the dotven , I used
require('dotenv').config({path: './configs/.env'});
This way my env variables were being accessed.
Hope this example of mine helps you! 😀

Related

Can't run script of MongoDB Atlas URL using node to database

I'm currently following along with a MDN tutorial Express Local Library Part 3. I've done everything correctly, that I know of, so far. I made a database then a cluster with MongoDB Atlas and am now trying to upload my data from a file populatedb.js by command node populatedb <my MongoDB Atlas URL here> but it doesn't work at all. I've linked it to the apps.js file with the correct URL (or so I think).
I'm getting this returned in the console:
___________-MacBook-Air express-locallibrary-tutorial % node populatedb 'mongodb+srv://_________:_______________#cluster0.mhzo6.mongodb.net/local_library?retryWrites=true&w=majority'
internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module '/Users/________/Desktop/express-locallibrary-tutorial/populatedb'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
I've even deleted my database, cluster, organization and everything and remade them exactly the same but still to no avail.
Here is the answer:
[for those of you stumped on this tutorial this will most likely be the cause!]
The tutorial made it very easy to misunderstand and create a secondary folder of the same name as the root in the root...[or perhaps and more likely its that we are still beginners and its very easy to misunderstand and make this simple mistake.] In creating the initial express project https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website at tutorial 2 "creating the project" I had accidentally made the simple mistake of creating my own express-locallibrary-tutorial folder/project in VS code then ran all of the cmds from the integrated terminal instead of the external terminal. This is what caused the double folder.
I was easily able to work around this just by making sure I was in the correct folder when running node/nodemon and made sure to path correctly for all routes just to "cover up" the mistake which I believed was no big deal... This caused me to have to trash my entire project. I had actually finished this tutorial but once it came to launching it to Heroku I quickly realized that it wasn't worth it to attempt to fix everything and best to restart it from the beginning because Heroku can only run scripts from the /root/ as it should [at least so far since this beginner/noob has been troubleshooting that is!]
So watch out for that simple mistake all! If you did it too and caught it early it is definitely worth the headache of restarting because it will help you in the long run and is a good lesson of a simple mistake, or it is for me!!

NodeJS App 'Module Not Found' when run from Docker

I'm currently in the process of containerizing my App. Part of the App is a NodeJS Express Backend.
It works without any issues when I run it on my console with node index.js or nodemon index.js.
Inside the middlewares/index.js I do import authJwt.js and verifySignup.js via require('./verifySignup) and require('./authJwt).
This works unsurprisingly well when I run my app from the console. However If i build my docker-image using the following Dockerfile-Code:
# build environment
FROM node:14
# Working Directoy
WORKDIR /app
ENV FILE_UPLOADS="./files/uploads"
ENV FILE_UPLOADS_PART="./files/uploads_part"
ENV PORT=3001
ENV DB_HOST="localhost"
ENV DB_USER="root"
ENV DB_PASSDWORD="root_password"
ENV DB_NAME="upload_db"
ENV SECRET_KEY="bezkoder-secret-key"
# Copy Needed Files for Dependencies
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock
# Install with YARN
RUN yarn install
# Copy Source Files
COPY ./ /app
EXPOSE 3001
CMD [ "yarn", "start" ]
When I run the Image it immediately terminates. Looking at the logs I find the following error:
$ docker logs 031241ce227a
yarn run v1.22.5
$ node index.js
internal/modules/cjs/loader.js:895
throw err;
^
Error: Cannot find module './verifySignUp'
Require stack:
- /app/middleware/middleware.js
- /app/routes/auth.routes.js
- /app/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
at Function.Module._load (internal/modules/cjs/loader.js:742:27)
at Module.require (internal/modules/cjs/loader.js:964:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/app/middleware/middleware.js:2:22)
at Module._compile (internal/modules/cjs/loader.js:1075:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1096:10)
at Module.load (internal/modules/cjs/loader.js:940:32)
at Function.Module._load (internal/modules/cjs/loader.js:781:14)
at Module.require (internal/modules/cjs/loader.js:964:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/app/middleware/middleware.js',
'/app/routes/auth.routes.js',
'/app/index.js'
]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I then exported my docker-image using docker export adoring_kowalevski > contents.tar to look at the filesystem inside the image, but everything is where it should be.
In a desperate attempt I hoped, that the issue might occur because I have multiple index.js in different folders, so I changed the names of all except the one in the root directory, but issue persisted. This is why in the error-log there is a middleware/middleware.js, it was after renaming the middleware/index.js.
Edit:
I tried using rfr to change my references from require('../middleware') to rfr('middleware'). It still workds from console but still not inside the container. So it seems that the issue doesn't occur because of the relative file paths.
Turns out I'm stupid (somewhat).
The import inside middleware/index.js looked like this require('./verifySignUp').
If we look at the file verifySignup.js inside the middleware Folder we can see, that it is named without the capital 'U'.
Why did it still work from console?
Inside verifySignup.js is the following piece of code:
module.exports = {
authJwt,
verifySignUp
};
Here it is written with capital 'U'. This means that running node index.js from the console only cared about whatever was written in the module.exports and not the actual Filename, however when running inside Docker, the Filename matters.
Renaming the file from verifySignup.js to verifySignUp.js solved my problems.

#loadable/server pass the whole stats JSON to eval('require')(modulePath)

I'm trying to setup SSR for react app with #loadable/components. I setup all based on docs with babel and webpack plugins. When I try to run node server.js it runs ok but when I open a browser and throws the following error (into node console):
TypeError [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received an instance of Object
at validateString (internal/validators.js:118:11)
at Module.require (internal/modules/cjs/loader.js:1033:3)
at require (internal/modules/cjs/helpers.js:72:18)
at smartRequire (/Users/max/Documents/repos/app/node_modules/#loadable/server/lib/util.js:44:25)
at new ChunkExtractor (/Users/max/Documents/repos/app/node_modules/#loadable/server/lib/ChunkExtractor.js:181:50)
at renderer (webpack://app/./node_modules/#MYSCOPE/elm/dist/elm.esm.js?:3619:19)
at eval (webpack://app/./src/server.tsx?:64:90)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 'ERR_INVALID_ARG_TYPE'
}
As you can see there is #MYSCOPE in the traceback which holds some of my internal packages (if it matters).
#loadable/server/lib/util.js is the following function:
And when I try to console.log(modulePath) on line 42 I see a whole stats JSON output which seems wrong and I should get a single module path (as I understand).
Any help?
I can share some specific parts of my configuration files if needed. Because I see my own package in console output seems like something is wrong with it's build (it works perfectly on the client-side with cjs build), but having full stats object as module path is very confusing.
UPD: Demo https://www.dropbox.com/s/9r947cgg4qvqbu4/loadable-test.zip?dl=0
Run
yarn
yarn dev:server
# go to localhost:3000 and see the error in console
to rebuild:
yarn
yarn dev:build-client
yarn dev:build-server
yarn dev:server # go to localhost:3000
The statsFile option passed to ChunkExtractor expects a path to the loadable-stats.json file, not the actual JSON content of it. By doing require('../loadable-stats.json'), webpack actually resolve the JSON during build time and assign it to the loadableJson variable.
You can change your loadableJson as follow:
import path from 'path';
const loadableJson = path.resolve(__dirname, '../bundle_client/loadable-stats.json');
This will solve the problem you had on your question. But, if you only do this, you will notice that you have another problem. Loadable by default assumes that your entry chunk name is main. This is not the case in your demo, as you have set the entry chunk name to be app instead.
entry: {
app: ['#babel/polyfill', './src/index.tsx']
},
To solve this, simply tell loadable about your entrypoints names by passing an array to the ChunkExtractor contructor as such:
const extractor = new ChunkExtractor({
statsFile: loadableJson,
entrypoints: ["app"], // name of your entry chunk
});
That's it, everything should now be working properly!
If it helps, I set up the demo on GitHub so you can easily see the changes I made here.

Change node path to current directory

I was trying to run this command from my user directory
NODE_ENV=~/Public/project node socket.js
But it return this error
Error: Cannot find module '/home/user/socket.js'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:418:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:533:3
The thing is i do not want to change directory. Is it possible to do that? And why this is happening?
NODE_ENV is, by convention, the type of environment you're running in, e.g. Development or Production or Test. It only matters for modules that inspect it and set options accordingly, e.g. Many loggers will default to more verbose in development.
Not really sure why you wouldn't just change directories, but your call to server.js can be an absolute or relative path. If the file is in the /home/user/project folder, you can (from /home/User) just call
node ./project/socket.js
After a little mor research, i found this
And according to that site, i have to do this
export NODE_CONFIG_DIR=/home/user/Public/project/config
and run this
node ./Public/smart-backwall-server/socket.js

NODE_PATH is being ignored or not working

I'm trying to run my node app on a new server and am having some issues with the NODE_PATH environment variable. The app works fine on my local machine (OSX Lion) but not on the server (Redhat linux). When starting my app with node app.js from within my project directory /development/nodeproject, I get the following error :
Error: Cannot find module 'mod/core/models/Category'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at /development/nodeproject/app.js:57:5
at Object.<anonymous> (/development/nodeproject/app.js:258:1)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
mod/core/models/Category is the first require() in my app.js and looks like this: var Category = require('mod/core/models/Category'). So apparently node is not looking for modules in my project directory.
I'm not sure why though, because I made the following changes (that are working just fine on my local machine).
added export NODE_PATH=/development/nodeproject to my ~/.bash_profile
ran source ~/.bash_profile
if I run env I see NODE_PATH=/development/nodeproject listed there
in my app.js if I console log process.env.NODE_PATH I get /development/framework (should this output an array instead of a string?)
Other information that might be relevant:
I'm on node v0.6.7
I'm doing all of this as root (sudo su -)
At this point I'm not sure what else I can do. Any help would be greatly appreciated.
NODE_PATH used for modules, not for solutions files.
Try
module.paths.push("/development/nodeproject", "one/more/path");
before any require() call. And you really should use a relative require like require('./mod/core/models/Category') for files in your nodeproject directory
The functionality you are looking for was removed. Use the node_modules directory or a relative require like require('./mod/core/models/Category').
This answer has more info: NODE_PATH error with node.js while attempting to setup jsctags for vim

Resources