user9632680
user9632680

Reputation:

How to insert the .env into a folder and run the nodemon?

SERVER.JS RESUME DOTENV

const dotenv   = require('dotenv-safe');
this.dotenv   = dotenv.load();

Problems:

1) I can not run the nodemon if it has only the .env file, it runs only if it contains the .env and .env.example files and I would like to know why and how to correctly match it.

2) How to insert the .env in the /env folder without the problem nodemon?

3) In my start script of package.json is the following "start_dev": "nodemon app/backend/src/start.js", however it is giving the following error:

nodemon app / backend / src / start.js
[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *. *
[nodemon] starting `node app / backend / src / start.js`
consign v0.1.6 Initialized in C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel
fs.js: 115
    throw err;
    ^

Error: ENOENT: no such file or directory, open '.env.example'
    at Object.openSync (fs.js: 436: 3)
    at Object.readFileSync (fs.js: 341: 35)
    C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel \ node_modules \ dotenv-safe \ index.js: 27: 45)
    at new Application (C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel \ app \ backend \ src \ config \ server.js: 11: 32)
    at-the-object. <anonymous> (C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel \ app \ backend \ src \ config \ server.js: 65: 18)
    at Module._compile (internal / modules / cjs / loader.js: 688: 30)
    at Object.Module._extensions..js (internal / modules / cjs / loader.js: 699: 10)
    at Module.load (internal / modules / cjs / loader.js: 598: 32)
    at tryModuleLoad (internal / modules / cjs / loader.js: 537: 12)
    at Function.Module._load (internal / modules / cjs / loader.js: 529: 3)
    at Module.require (internal / modules / cjs / loader.js: 636: 17)
    at require (internal / modules / cjs / helpers.js: 20: 18)
    at aliasce-app-analyticals-panel \ app \ backend \ src \ start.js: 1: 78)
    at Module._compile (internal / modules / cjs / loader.js: 688: 30)
    at Object.Module._extensions..js (internal / modules / cjs / loader.js: 699: 10)
    at Module.load (internal / modules / cjs / loader.js: 598: 32)
[nodemon] app crashed - waiting for file changes before starting ...

And if I run the nodemon in the C:\Users\username\Documents\DEVELOPMENT\NEORIS\ ALIANSCE\aliansce-app-analyticals-panel\app\ ackend\src directory It works!

enter image description here

Upvotes: 0

Views: 1893

Answers (2)

giran
giran

Reputation: 378

With help of @OlegDover and yargs, I managed to pass .env file from a different path and use nodemon for hot-reloading during development.

e.g. $ nodemon --watch /path/to/.env server.js --envPath=path/to/.env will pick up changes to .env file and restart deployment.

Example Code

.env

EXAMPLE_HOST=myhost
EXAMPLE_PORT=5566

env.js

/*
 * Module dependencies
 */

const fs = require("fs");
const yargs = require("yargs");

/*
 * Custom class to update process.env from custom filepath
 * Ref: https://github.com/olegdovger/pizza-delivery-api/blob/master/lib/env.js
 */

class Env {
  constructor(envPath) {
    this.variables = [];
    this._setup(envPath);
  }

  _setup(envPath) {
    try {
      const data = fs.readFileSync(envPath, {
        encoding: "utf-8",
      });
      const stringArray = data.split("\n");
      this.variables = stringArray.map((string) => {
        const arr = string.split("=");
        return {
          name: arr[0],
          value: arr[1],
        };
      });
    } catch (err) {
      console.error("Unable to load .env;", err);
    }
  }

  load() {
    this.variables.forEach((variable) => {
      process.env[variable.name] = variable.value;
    });
  }
}

/*
 * Load .env from argv filepath
 */
const argv = yargs.argv;
new Env(argv["envPath"]).load();

/**
 * Register
 */
module.exports = {
  EXAMPLE_HOST: process.env.EXAMPLE_HOST || "localhost",
  EXAMPLE_PORT: Number(process.env.EXAMPLE_PORT) || 12345,
};

server.js

const { EXAMPLE_HOST, EXAMPLE_PORT } = require("./env");

The environment variables can then be loaded/used in the project, having the fallback values if they are not defined in the .env file.

i.e. if EXAMPLE_HOST is not present in .env, this value will default to localhost

Upvotes: 0

OlegDovger
OlegDovger

Reputation: 122

I looked at this line of code - https://github.com/rolodato/dotenv-safe/blob/master/index.js#L27

It tries to read file .env.example but can not find it in the current folder (run pwd to check it)

It might be 3 ways to solve issue

1) Run

cd app/backend/src
nodemon start.js

2) Move files .env, .env.example to parent folder (aliansce-app-analyticals-panel) and then run nodemon app/backend/src/start.js

3) Do not use dotenv-safe at all. Use your own simple script like this one

Just look at this simple example:

Upvotes: 2

Related Questions