FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

src: add `process.loadEnvFile` and `util.parseEnv` by anonrig · Pull Request #51476 · nodejs/node · GitHub

/ node Public

src: add process.loadEnvFile and util.parseEnv - #51476

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
anonrig:add-load-env-file
Jan 23, 2024
Merged

src: add process.loadEnvFile and util.parseEnv#51476
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
anonrig:add-load-env-file

Conversation

anonrig commented Jan 15, 2024
edited
Loading

Copy link
Copy Markdown
Member

Introduces 2 new functions to use the default .env parser.

process.loadEnvFile(path)

  • In order to load the .env file in the current directory:
process.loadEnvFile()
  • Load a specific path:
process.loadEnvFile('./development.env')

util.parseEnv(content)

  • In order to parse an existing string:
assert.deepStrictEqual(require('node:util').parseEnv('HELLO=world'), { HELLO: 'world' });

Fixes #51413
Ref #49148


Thank you Sentry for sponsoring this work!

anonrig added semver-minor PRs that contain new features and should be released in the next minor version. notable-change PRs with changes that should be highlighted in changelogs. labels Jan 15, 2024

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/gyp
  • @nodejs/startup

Copy link
Copy Markdown
Contributor

The notable-change PRs with changes that should be highlighted in changelogs. label has been added by @anonrig.

Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.

nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Jan 15, 2024
anonrig mentioned this pull request Jan 15, 2024
8 tasks
anonrig force-pushed the add-load-env-file branch 3 times, most recently from 0c30e62 to 00927c3 Compare January 15, 2024 19:45
anonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 15, 2024
github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 15, 2024

Copy link
Copy Markdown
Collaborator

aduh95 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Can you please add a test that validates what happens when the .env file defines a NODE_OPTIONS?

GeoffreyBooth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I just reviewed the docs, but 👍 to the general idea. Thanks for implementing this.

Comment thread doc/api/util.md Outdated
Comment thread doc/api/util.md Outdated
Comment thread doc/api/util.md Outdated
Comment thread doc/api/util.md Outdated
GeoffreyBooth added util Issues and PRs related to the built-in util module. process Issues and PRs related to the process subsystem. cli Issues and PRs related to the Node.js command line interface. labels Jan 15, 2024
Comment thread doc/api/util.md Outdated
targos previously requested changes Jan 16, 2024

targos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It seems more logical to me to have it on process, since it has direct effect on process.env. An utility function would only return the parsed result without side effects

Comment thread lib/util.js Outdated
Comment thread lib/util.js Outdated

anonrig commented Jan 16, 2024

Copy link
Copy Markdown
Member Author

It seems more logical to me to have it on process, since it has direct effect on process.env. An utility function would only return the parsed result without side effects

@targos Process is a global variable. Are you suggesting having process.loadEnvFile()?

targos commented Jan 16, 2024

Copy link
Copy Markdown
Member

That's what I'm suggesting, yes.

arthurfiorette commented Jan 16, 2024
edited
Loading

Copy link
Copy Markdown

What about also adding a "register" like import which simply consists of the following file:

import util from 'node:util'
util.loadEnvFile()

which allows it to be used as follows:

import 'node:util/load-env'

Ofc load-env was just a random name I chose. It's similar to how dotenv/register works, which would ease migration to this native node way.

Copy link
Copy Markdown
Member

Ofc load-env was just a random name I chose. It’s similar to how dotenv/register works, which would ease migration to this native node way.

I don’t think “be able to load environment variables from a file in a one-liner” is a high priority. I think we should separate out the loading from the parsing, and this should just be parseEnvFile and it can stay on util. This takes care of the other request that someone had, to be able to load multiple files.

import { parseEnvFile } from 'node:util'
import { dev } from './my-environment.js'

const commonVariables = await readFile('.env', 'utf8')
const environmentSpecificVariables = await readFile(`.${dev ? 'dev' : 'prod' }.env`, 'utf8')
const parsedCommonVariables = parseEnvFile(commonVariables)
const parsedEnvironmentSpecificVariables = parseEnvFile(environmentSpecificVariables)
process.env = { ...process.env, ...parsedCommonVariables, ...parsedEnvironmentSpecificVariables }

MoLow left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM. I agree it makes sense to move to process

anonrig changed the title util: add loadEnvFile programmatic API src: add process.loadEnvFile programmatic API Jan 16, 2024
marco-ippolito mentioned this pull request Mar 1, 2024
marco-ippolito added a commit that referenced this pull request Mar 1, 2024
Notable changes:

build:
  * (SEMVER-MINOR) build opt to set local location of headers (Michael Dawson) #51525
crypto:
  * (SEMVER-MINOR) implement crypto.hash() (Joyee Cheung) #51044
  * update root certificates to NSS 3.98 (Node.js GitHub Bot) #51794
doc:
  * add zcbenz to collaborators (Cheng Zhao) #51812
  * add lemire to collaborators (Daniel Lemire) #51572
http2:
  * (SEMVER-MINOR) add h2 compat support for appendHeader (Tim Perry) #51412
  * (SEMVER-MINOR) add server handshake utility (snek) #51172
  * (SEMVER-MINOR) receive customsettings (Marten Richter) #51323
lib:
  * (SEMVER-MINOR) move encodingsMap to internal/util (Joyee Cheung) #51044
sea:
  * (SEMVER-MINOR) support sea.getRawAsset() (Joyee Cheung) #50960
  * (SEMVER-MINOR) support embedding assets (Joyee Cheung) #50960
src:
  * (SEMVER-MINOR) print string content better in BlobDeserializer (Joyee Cheung) #50960
  * (SEMVER-MINOR) support multi-line values for .env file (IlyasShabi) #51289
  * (SEMVER-MINOR) add `process.loadEnvFile` and `util.parseEnv` (Yagiz Nizipli) #51476
  * (SEMVER-MINOR) do not coerce dotenv paths (Tobias Nießen) #51425
stream:
  * (SEMVER-MINOR) implement `min` option for `ReadableStreamBYOBReader.read` (Mattias Buelens) #50888
util:
  * (SEMVER-MINOR) add styleText API to text formatting (Rafael Gonzaga) #51850
vm:
  * (SEMVER-MINOR) support using the default loader to handle dynamic import() (Joyee Cheung) #51244

PR-URL: #51932
marco-ippolito added a commit that referenced this pull request Mar 1, 2024
Notable changes:

build:
  * (SEMVER-MINOR) build opt to set local location of headers (Michael Dawson) #51525
crypto:
  * (SEMVER-MINOR) implement crypto.hash() (Joyee Cheung) #51044
  * update root certificates to NSS 3.98 (Node.js GitHub Bot) #51794
doc:
  * add zcbenz to collaborators (Cheng Zhao) #51812
  * add lemire to collaborators (Daniel Lemire) #51572
http2:
  * (SEMVER-MINOR) add h2 compat support for appendHeader (Tim Perry) #51412
  * (SEMVER-MINOR) add server handshake utility (snek) #51172
  * (SEMVER-MINOR) receive customsettings (Marten Richter) #51323
lib:
  * (SEMVER-MINOR) move encodingsMap to internal/util (Joyee Cheung) #51044
sea:
  * (SEMVER-MINOR) support sea.getRawAsset() (Joyee Cheung) #50960
  * (SEMVER-MINOR) support embedding assets (Joyee Cheung) #50960
src:
  * (SEMVER-MINOR) print string content better in BlobDeserializer (Joyee Cheung) #50960
  * (SEMVER-MINOR) support multi-line values for .env file (IlyasShabi) #51289
  * (SEMVER-MINOR) add `process.loadEnvFile` and `util.parseEnv` (Yagiz Nizipli) #51476
  * (SEMVER-MINOR) do not coerce dotenv paths (Tobias Nießen) #51425
stream:
  * (SEMVER-MINOR) implement `min` option for `ReadableStreamBYOBReader.read` (Mattias Buelens) #50888
util:
  * (SEMVER-MINOR) add styleText API to text formatting (Rafael Gonzaga) #51850
vm:
  * (SEMVER-MINOR) support using the default loader to handle dynamic import() (Joyee Cheung) #51244

PR-URL: #51932
richardlau mentioned this pull request Mar 25, 2024
Burkieboy mentioned this pull request Apr 2, 2024
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no outstanding review comments, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. cli Issues and PRs related to the Node.js command line interface. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. notable-change PRs with changes that should be highlighted in changelogs. process Issues and PRs related to the process subsystem. semver-minor PRs that contain new features and should be released in the next minor version. util Issues and PRs related to the built-in util module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a feature to parse .env file by default

Back | FazBrowse Home | New Git URL