Hi,
I am trying to use dotenv module to set my environment variables in Loopback 4 project setup. But this is causing build error
I have included
require('dotenv').config()
in index.js file present in the root of the Loopback 4 project.
Error
error TS5033: Could not write file 'dist/index.d.ts': EPERM: operation not permitted, mkdir '{path}dist'.
Any solution to fix this problem ?
Hi,
You can do this in the contstructor of the application.ts file.
import * as dotenv from 'dotenv';
[...]
constructor(options: ApplicationConfig = {}) {
super(options);
// Set up dotenv
dotenv.config({ path: '.env' });
[...]
}
Hi,
The issue got resolved after I deleted node_modules and package-lock.json and installed node_modules again along with @types/dotenv package.
Thanks
@youritornier Thank you for this approach.
Honestly this approach worked till it didn't and I am not sure why. My datasources file where I was using env variables were getting loaded first before my dotenv.config() worked.
The other workaround which is sure to work every time is modifying the start script in package.json
Change from "start": "node -r source-map-support/register ." to "start": "node -r dotenv/config -r source-map-support/register ."
This method is guaranteed to work and is mentioned in dotenv's README as well. Just posting for whoever comes searching for this in the future.
I've had to configure dotenv in index.ts file instead:
import * as dotenv fom 'dotenv'
....
if(require.main === module) {
// dotenv config
dotenv.config({path: '.env'})
...
const config = {
...
Most helpful comment
Hi,
You can do this in the contstructor of the
application.tsfile.