How do I get my Nodejs .env file recognized? - node.js

Well I started to learn how to create api rest
and when I want to call it in the proccess.env it doesn't recognize it.
I already have dotenv installed
this is how my .env file looks like
this is the error, I have tried with other ports but I get the same error .
my package.json
I also tried following:
require('dotenv').config({path: '../.env'});
require('dotenv').config({path: '../../.env'});
and all I get is this
enter image description here
I hope I made myself understood, I tried to search on yt but found no answer. Thank you for your time

You do not need to write it like that, you can just use it like this
import * as dotenv from 'dotenv'
dotenv.config()
this is possible because the default path is your root folder.
Also, you are supposed to put it in your root folder as it is stated by the creators, you can read it here.
However if you really want to use it in another directory, you can read through solution on this question.

Related

Editing the .env file using node.js

I have a check.env file, in which I have some variables like
SharedAccessKey=
I want to put a value in the check.env file from my node.js code. Articles on internet are there for updating at the running time of node.js, but my requirement is to change in the file and keep the file with changes made.
How can I accomplish that.
I got this link : How to change variables in the .env file dynamically in Laravel?
but it is in some other language, how can I do in node.js.
I was unable to find out the best solution so went with another solution of mine that I took.
I am using two files now both .env extensions and I am copying main .env file to another empty .env file (like check1.env to check2.env).
Any modifications I am making is in the second file (check2.env).
And I am using string replacement in the .env file, using fs.readLine() and getting the string and the data.replace(), this worked for me.
The reason to use two .env files is that even if I change in the second file, again by copying from the first file I will get same string search and will replace with a different value.
-- Please suggest if there is an any better approach. Thanks

Error: The app has not been configured with a WORKSPACE_ID environment variable

I've changed the workspace ID in both my env file as well as my app.js file just in case. I've installed all the necessary SDK's and have done everything according to the instructions here.
I swapped out the workspace files with my own workspace. But I made all the necessary changes including changing the workspace ID. Not quite sure why i'm still getting the error. Any help would be much appreciated!
Edited: Here are the changes I made to my code
Image
I had the same problem a few days ago. And, to solved this, you can paste the WORKSPACE_ID in this line too!
But, if you want use the .env file, make sure the are no spaces between = and the end of the line.
I will put the print of my app.js as an example to help you:
Actually, both ways work.
My example show this:
username = "<iwdiaowd-jioawjd-ioajgr-ogxgxajofa>"
password = "<xxxxxxxxxxxxxxxxxxxx>"
var workspace = "<59583409583495834590385394>"`
Try edit your .env:
WLP_SKIP_MAXPERMSIZE=true
VCAP_SERVICES={"conversation": [{"name": "conversation-service","label": "conversation","plan": "free","credentials": {"url": "https://gateway.watsonplatform.net/conversation/api","isStreaming": false,"password": "-ReplaceMe-","username": "-ReplaceMe-"}}]}
COLLECTION_NAME=workspace
WORKSPACE_ID=iwdiaowd-jioawjd-ioajgr-ogxgxajofa
LOGGING_ENABLED=false

learning node and modules / creating a file using

Im really new to node and feel like I understand nothing. I was watching a tutorial where the fs.writeFileSync creates a new file. However my code doesn't work and no file is being created-does someone know why? Also why do I need
var fs=require("fs");
? As I understand fs is a build in module and if we require something, there should be another file that exports something (which we require using the fs module)? Node is kind of hard to understand and would appreciate some explanation! Thanks
var fs=require("fs");
fs.writeFileSync("contents.txt","Thats a new file")
console.log(fs.writeFileSync("contents.txt").toString());
fs is indeed a node built-in module, and as other modules, you must require it to use its capabilities. the file you're referring is present internally, so you don't have to npm install it.
And regarding your code, fs.writeFileSync should work as you used it, however, when you tried to print it, you used this function again, this time with no contents, what probably caused the mix-up.
Code that should work perfectly is:
//Requiring the fs module in order to use it later on
var fs = require('fs');
//Writing "Thats a new file" as text to a new file called "contents.txt" in the same directory as the script file.
fs.writeFileSync('contents.txt', 'Thats a new file');
//If you want to print the file, read it, like so.
console.log(fs.readFileSync('contents.txt'));
Also, I think you should continue reading about node's async capabilities so you can understand better this technology and what is it good for. This is one site you can learn from but there are a lot of other good ones out there.

Can't find mongoose module NodeJS require

for some reason I can't get my require to work, it should just find both models fine but the path for the file just won't work.
app
models
user.js
match.js
server.js
Seems like a simple fix but cannot seem to do it myself right now. I'm using (var User = require('/app/models/user.js');)
Thanks guys.
Actually you also can write it as,
var User = require('./app/models/user')
without even putting .js at the end, as it is added by default.. Just a tip to know :)
and here ./ means the current directory.
Hope it helps :)

ENOENT no such file on Express Endpoint for readFileSync

I've been going slightly crazy trying to figure this out. I have some certs that I need to pass through to an authentication client from my api; however, the application continues to throw ENOENT exceptions even though the file clearly exists within the same directory (I've fiddled with this to make sure). I'm using readFileSync, effectively doing the following:
key: fs.readFileSync('./privateKey.pem'),
Strangely, if I run this on a standalone Node server not as a part of an api, the file is able to be found without a problem. Is there some consideration I'm not aware of when trying to use readFileSync in such a scenario?
Thanks!
In node you need to be very careful with relative file paths. The only place where I'd ever really use them is in require('./_____') statements, where ./ to mean "relative to this file". However, require is kind of a special case because it is a function that node automatically creates per-file, so it knows the path of the current file.
In general, standard functions have no way of knowing the directory containing the script that happened to call a function, so in almost all cases, ./ means relative to the current working directory (the directory you were in when you ran node <scriptname>.js). The only time that is not the case is if your script or a module you use explicitly calls process.chdir to set the working directory to something else. The correct way to reference files relative to the current script file is to explicitly use an absolute path by using __dirname + '/file.js'.

Resources