Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 502 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use nodemon with .env files?

#11
To load the dotenv package and any declared .env vars into the environment, you can do the following:

nodemon -r dotenv/config myapp.js
Reply

#12
If you want to run Typescript in `nodemon` *and* require a *particular* `.env` file with `dotenv` then you can do:

In `package.json` scripts:

`"dev": "nodemon -r dotenv/config src/myApp.ts dotenv_config_path=/path/to/your/env/file",
`


And a line in `nodemon.json` to tell `nodemon` to use `ts-node` when encountering Typescript extensions:

`"execMap": {"ts": "node -r ts-node/register"},`

This is useful for using a development `.env` file say `.env.development.local` for local dev work and leave the main `.env` file for live production variables.
Reply

#13

"scripts": {
"start": "node -r dotenv/config src/server.js dotenv_config_path=dev.env dotenv_config_debug=true",
"start:dev": "nodemon --exec \"npm start\""
}
Reply

#14
I use `cross-env` for environments.

`npm i cross-env`

set `package.json`.

"start": "cross-env NODE_ENV=production node dist/app.js",
"dev": "cross-env NODE_ENV=dev nodemon --exec ts-node src/app.ts",

`npm run start` OR `npm run dev`
Reply

#15
This will do it,
```sh
nodemon -w . -w .env index.js
```

How it works:
**"-w ."** tells nodemon to watch the files in the current directory
**"-w .env"** tells nodemon to watch the .env file
**"index.js"** is just the file to run when changes occur (could be anything)
Reply

#16
in `package.json`:
```
...
"scripts": {
...
"dev": "nodemon src/index.js -w . -w .env -e js,mjs,json,env",
...
},
...
```
assuming `index.js` is located at `./src` and `.env` file is located at root.

`npm run dev` now:
```
[nodemon] watching path(s): *.* .env
[nodemon] watching extensions: js,mjs,json,env
```
Reply

#17
In my case I'm using TypeScript and the `.env` file is used for development only. I wanted my code to be decoupled from the `.env` file and I didn't want to `import 'dotenv/config'` anywhere in my code. This is my solution:

My nodemon config:
```json
{
"watch": [
"src",
".env"
],
"ext": ".ts",
"exec": "ts-node -r dotenv/config ./src/index.ts"
}
```

My NPM script:
```json
"start:dev": "nodemon"
```

In this solution `ts-node` requires dotenv, which sets up the environment variables before the main app starts. This means that nowhere in my code do I need a `import 'dotenv/config'`. `dotenv` can become a dev dependency, and this also prevents dotenv to be loaded at all once the code is deployed.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through