"dotenv: error when sourcing '.env' file"? - node.js

Every time I cd a particular directory using either Terminal or iTerm2 (I'm running Macos 10.14.5, Node 12.13.0), I get the following error that seems to be complaining about an ´.env´ file provided to me by my team. They are not getting the error.
.env:22: parse error near '}'
dotenv: error when sourcing '.env' file
.env:22: parse error near '}'
Line 22, which seems to be causing the error is
COOKIE_SETTINGS={"isSecure": false, "isSameSite": false, "isHttpOnly": true, "encoding": "base64json", "path": "/"}
Any clue how I might remedy this? Thanks for any insights.

You can't store objects inside .env file.
The solution is to:
Stringify JSON object and save as env variable. Then parse and use it when you need your object.
Another solution is to write variables like this:
COOKIE_SETTINGS_IS_SECURE = false
COOKIE_SETTINGS_IS_SAME_SITE = false
...

This seem error when parsing env.
I think env just support like string or number for this case.
Maybe you can use in .env
COOKIE_SETTINGS="{'isSecure': false, 'isSameSite': false, 'isHttpOnly': true, 'encoding': 'base64json', 'path': '/'}"
In JS just use JSON.parse to turn back to Object
cookieSettings= process.env.DB_HOST ? JSON.parse(process.env.DB_HOST) : null;

Related

vite - module is not defined error in 3rd party npm package

Here's the error I'm getting inside of my application:
Uncaught ReferenceError: module is not defined
at isHotReloading2 (isHotReloading.js:2:20)
at Form3.UNSAFE_componentWillMount (createReduxForm.js:511:16)
and here's what the error looks like in the chrome inspector:
I can't easily change the course code of redux-form (which is no longer being maintained) and neither can I remove it from my application. Is there a way to work around this error?
I've tried the following fixes in the vite.config.js file to no avail. Any ideas would be greatly appreciated. Thanks!
I ran into this same issue with Redux-form and fixed it as follows.
Create a file with the following
const isHotReloading = () => false;
export default isHotReloading;
In your vite.config file add the following to resolve.alias
{
find: './util/isHotReloading',
replacement: path.resolve(__dirname, './PATH_TO_FILE_ABOVE/reduxFormHotReload.js'),
},
This will disable the hot reload update functionality all together, so maybe you could improve the function above, but I didn't worry about it.

Issue with running PDF.JS

I am looking to create a simple program that prints the text contents of a pdf file to the screen using the library pdf.js (https://www.npmjs.com/package/pdfjs-dist)
So far, I have an index.js file that is calling let PDFJS = require('pdfjs-dist'); I installed pdf.js using via the command 'npm i pdfjs-dist'. Problem is, as soon as I tried to run my index.js file 'run index.js' I am hit with the following error
/path/to/node_modules/pdfjs-dist/build/pdf.js:1361
await this._transport?.destroy();
^
SyntaxError: Unexpected token '.'
I'd like to know if anyone else had faced a similar issue when using pdf.js (will provide link) and if so, how to fix it? is there something that I require before using pdf.js?
any help would be very helpful.

Jasming config flag (--config) does not use the right config file

I have my node server on path F:\proj\dev-react-node-java\src\server. I used 'jasmine init' to create spec folder here and running 'jasmine' in terminal runs the specs (tests) correctly.
I wish to run the tests from F:\proj\dev-react-node-java so I used the command
jasmine --config=src/server/spec/support/jasmine.json
at this path but I get the message 'No specs found'. Why is it not using the correct configuration file (jasmine.json)?
I am sure --config reaches for this file because:
Giving wrong path gives 'Cannot find module' error.
Writing errorful json also generates and error.
Here is the jasmine.json code for reference:
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": true
}
spec/support/jasmine.json is the default path as far as I understand since running 'jasmine' command at path say F:\proj\dev-react-node-java\src\server\spec also results in No specs found.
jasmine version is 3.6.1
P.S. This is my first question asked here. Please inform if I made any mistakes in asking. Thank you.
I did find the reason. It is indeed not an issue with the config flag but rather with my jasmine.json file.
What I thought the use of config flag was to specify the path to the file instead of the default spec/support/jasmine.json. It would then have the same behaviour as if the relative path to config was spec/support/jasmine.json.
But
F:\proj\dev-react-node-java>jasmine --config=src/server/spec/support/jasmine.json
is not the same as
F:\proj\dev-react-node-java\src\server>jasmine --config=spec/support/jasmine.json
What it does instead is like copying it to the path from where the command was called and then using it to run the tests.
Hence, what worked was changing the spec_dir field.
{
"spec_dir": "src/server/spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": true
}
A little more clarification/examples in the docs would have been nicer but perhaps I misunderstood the functionality.

NodeJS not recognizing .env file

I have like 5 NodeJS services running, but I have a problem in one of those.
This is the nodemon.json file:
{
"watch": ["**/*.ts"],
"ext": "ts,json",
"ignore": ["./test/*.ts"],
"exec": "node -r ts-node/register -r dotenv/config Index.ts dotenv_config_path=$(pwd)/.env",
"env": {
"NODE_ENV": "development"
}
}
It's the same as the rest of services. When I run npm run dev I got error messages depending on which value is taking from the .env file, example:
const LOCAL_CONFIGURATION = {
PORT_APP: 8082,
MONGODB: {
SERVER: process.env.MONGO_DTE,
AUTH: {
auth: {
password:process.env.MONGO_PASSWORD,
user:process.env.MONGO_USER
}
},
},
MS_NOTIFICACION: "http://localhost:8089/notificacion",
ELASTIC_PATH: process.env.ELASTIC_PATH,
...COMMON,
};
The first error message is:
ConfigurationError: Missing node(s) option
That message is produced because it's not reading the value from process.env.ELASTIC_PATH, but if I put a hardcoed value like "http://with.the.correct.url" and it tries again to run, I get another error:
Error: Credentials must be provided when creating a service client
That error is because it's trying to read password:process.env.MONGO_PASSWORD and user:process.env.MONGO_USER
etc, so, there's a problem on reading the .env file. I know that .env file has those values, and the file is in UTF-8, without quotes, etc. The .env file is the same file as the other services, it works ok in the rest but I don't know why is not getting read here.
Any idea?
EDIT:
Plus, I put a console.log(process.env); in config.ts file and it shows values like this:
But there's no values from the .env for example, there in the picture there's a value called COMPUTERNAME so if I put console.log(process.env.COMPUTERNAME); I get: IBM-NOT87
Why is not getting the .env file?
Seems like you need to require/configure dotenv. Docs:
As early as possible in your application, require and configure dotenv.
require('dotenv').config()
To further expand on #JBallin answer
you should use this on your app.js
Or if that does not work then you will need to explicitly add it to the file you are wanting to use those Variables
Sharing image, as its sometimes easier to see expanded
code here =>
require('dotenv/config') // require the dotenv/config at beginning of file
const express = require('express')
const mongoose = require('mongoose')
require('dotenv').config({ path: "./sample.env" });
In the file you are using environment variables,
As early as possible, require the "dotenv" and in the config() method, specify the path of the .env file, even if it in your root directory or the same directory where node starts.
The code for requiring and specifying file in the same directory is in the first line in the answer.
Also, for further reading 📖 , you can visit https://github.com/motdotla/dotenv#path
You cat try this.
-> npm i dotenv
and in code add this piece of code
require('dotenv').config({
path: 'your path here'
})
Install dotenv package
npm install --s dotenv
And add this require("dotenv").config(); in index.js/ts file.

Path is undefined,

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
What could be the possible reason? I searched a lot, it is giving error for a particular function.
here`s the first line of the function that is being called
path.resolve(__dirname, 'templates');
The error is because __dirname is coming as undefined. __dirname only works for scripts. If you're trying to run it in REPL it won't work and i think you're doing that.
create a new file test.js and copy paste the line below in it
console.log(path.resolve(__dirname, 'templates'));
and run it:
node test.js
Hope this helps

Resources