8

I am trying to preload the .env file when the node application starts.

I have the following configuration but whenever I run npm run start:dev i get error.

"scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "prestart": "npm run build",
    "start": "node .",
    "start:dev": "node -r dotenv/config . dotenv_config_path=/.dev.env",
    "start:test": "node -r dotenv/config . dotenv_config_path=/.test.env",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

or

"scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "prestart": "npm run build",
    "start": "node .",
    "start:dev": "node -r dotenv/config . dotenv_config_path=./.dev.env",
    "start:test": "node -r dotenv/config . dotenv_config_path=./.test.env",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

My app structure looks like following

enter image description here

Also I have read about not to use multiple config files so I am aware of the drawbacks but I just want to try and see how this will work.

Any help will be appreciated. Thanks!!!

Update#1 -- Error

Error: Cannot find module 'C:\Development\index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)
    at Function.Module._load (internal/modules/cjs/loader.js:506:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! development@1.0.0 start:dev: `node -r dotenv/config index.js dotenv_config_path=/.dev.env`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the development@1.0.0 start:dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\nikhil\AppData\Roaming\npm-cache\_logs\2019-06-05T10_11_48_699Z-debug.log

Log file ---

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'start:dev' ]
2 info using npm@6.4.1
3 info using node@v10.13.0
4 verbose run-script [ 'prestart:dev', 'start:dev', 'poststart:dev' ]
5 info lifecycle development@1.0.0~prestart:dev: development@1.0.0
6 info lifecycle development@1.0.0~start:dev: development@1.0.0
7 verbose lifecycle development@1.0.0~start:dev: unsafe-perm in lifecycle true
8 verbose lifecycle development@1.0.0~start:dev: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Development\node_modules\.bin;
9 verbose lifecycle development@1.0.0~start:dev: CWD: C:\Development
10 silly lifecycle development@1.0.0~start:dev: Args: [ '/d /s /c',
10 silly lifecycle   'node -r dotenv/config index.js dotenv_config_path=/.dev.env' ]
11 silly lifecycle development@1.0.0~start:dev: Returned: code: 1  signal: null
12 info lifecycle development@1.0.0~start:dev: Failed to exec start:dev script
13 verbose stack Error: development@1.0.0 start:dev: `node -r dotenv/config index.js dotenv_config_path=/.dev.env`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:182:13)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:182:13)
13 verbose stack     at maybeClose (internal/child_process.js:962:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
14 verbose pkgid development@1.0.0
15 verbose cwd C:\Development
16 verbose Windows_NT 10.0.16299
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "start:dev"
18 verbose node v10.13.0
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 1
22 error development@1.0.0 start:dev: `node -r dotenv/config index.js dotenv_config_path=/.dev.env`
22 error Exit status 1
23 error Failed at the development@1.0.0 start:dev script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
planet_hunter
  • 3,866
  • 1
  • 26
  • 39
niklodeon
  • 1,320
  • 5
  • 20
  • 51
  • 1
    what is the error you get? – Harshith Rai Jun 05 '19 at 10:20
  • @Rai updated the question with error and log file. – niklodeon Jun 05 '19 at 11:27
  • Would `node -r dotenv/config dotenv_config_path=./.dev.env` work? try this from command line first, then set up a script in your package.json – bracco23 Jun 05 '19 at 12:29
  • @bracco23 doesn't work `internal/modules/cjs/loader.js:582 throw err; ^ Error: Cannot find module 'C:\Development\dotenv_config_path=\.test.env' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15) at Function.Module._load (internal/modules/cjs/loader.js:506:25) at Function.Module.runMain (internal/modules/cjs/loader.js:741:12) at startup (internal/bootstrap/node.js:285:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)` – niklodeon Jun 05 '19 at 13:10
  • @NikhilGupta, the error `Error: Cannot find module 'C:\Development\index.js'` says it can't find some file.Are you sure your file exists in that path itself? If you are, then check the spellings... if still not okay, fially check the extension of `index` file if it is a `.ts` or a `.js` file. – Harshith Rai Jun 06 '19 at 04:42
  • @Rai, folder structure is same as in pic in the question. so files are .ts and not .js. Also when i try `node -r dotenv/config .` i get similar error `internal/modules/cjs/loader.js:582 throw err; ^ Error: Cannot find module 'C:\Development' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15) at Function.Module._load (internal/modules/cjs/loader.js:506:25) at Function.Module.runMain (internal/modules/cjs/loader.js:741:12) at startup (internal/bootstrap/node.js:285:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)` – niklodeon Jun 06 '19 at 07:16
  • Also is the usage of dotenv different when using in a typescript project – niklodeon Jun 06 '19 at 07:17
  • 1
    I found the problem. In the package.json I only have a start:dev command which tries to start the server but there is no prestart:dev command which will build the project. start has a prestart as well so everything works fine there. – niklodeon Jun 06 '19 at 08:01
  • sounds good buddy. – Harshith Rai Jun 06 '19 at 08:25

2 Answers2

5

Problem was with the script section in the package.json file.

There was no prestart:dev which would build the project before starting the server.

  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "prestart": "npm run build",
    "prestart:dev": "npm run build",
    "prestart:test": "npm run build",
    "start": "node -r dotenv/config . dotenv_config_path=./.env",
    "start:dev": "node -r dotenv/config . dotenv_config_path=./.env.dev",
    "start:test": "node -r dotenv/config . dotenv_config_path=./.env.test",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
niklodeon
  • 1,320
  • 5
  • 20
  • 51
3

You can use the dotenv-cli package, really easy to use :

Here's an example of how I use it :

"create:db:dev:windows": "SET NODE_ENV=development& dotenv -e .env.local sequelize db:create && npx sequelize-cli db:migrate",
Yoann Buzenet
  • 651
  • 2
  • 5
  • 12