{"id":17472840,"url":"https://github.com/sneakertack/nvar","last_synced_at":"2025-08-13T11:47:30.600Z","repository":{"id":77593046,"uuid":"96834715","full_name":"sneakertack/nvar","owner":"sneakertack","description":"Reads shell environment variables from a file and assigns them to `process.env` (or anywhere else).","archived":false,"fork":false,"pushed_at":"2018-04-27T06:17:04.000Z","size":65,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-23T02:20:30.897Z","etag":null,"topics":["config","configuration-management","env","environment-variables","init","node","settings","shell"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sneakertack.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-07-11T00:55:55.000Z","updated_at":"2021-08-13T18:36:20.000Z","dependencies_parsed_at":"2023-05-23T00:00:23.914Z","dependency_job_id":null,"html_url":"https://github.com/sneakertack/nvar","commit_stats":{"total_commits":63,"total_committers":2,"mean_commits":31.5,"dds":0.04761904761904767,"last_synced_commit":"79c7da36bacdbb6ac239e841c4a4d6de5862bc59"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/sneakertack/nvar","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sneakertack%2Fnvar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sneakertack%2Fnvar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sneakertack%2Fnvar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sneakertack%2Fnvar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sneakertack","download_url":"https://codeload.github.com/sneakertack/nvar/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sneakertack%2Fnvar/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261602169,"owners_count":23183208,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","configuration-management","env","environment-variables","init","node","settings","shell"],"created_at":"2024-10-18T17:39:33.206Z","updated_at":"2025-06-29T12:33:37.934Z","avatar_url":"https://github.com/sneakertack.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/sneakertack/nvar.svg?branch=master)](https://travis-ci.org/sneakertack/nvar) ![A static test-count badge (dynamise one day)](https://img.shields.io/badge/tests-58%2F58-brightgreen.svg) [![Coverage Status](https://coveralls.io/repos/github/sneakertack/nvar/badge.svg?branch=master)](https://coveralls.io/github/sneakertack/nvar?branch=master)\n\n## Intro\n\n`nvar` is a Node module that lets you declare environment variables in an envfile (usually `.env` in your app's root folder). When your app starts, `nvar` loads those variables (into `process.env` by default), making it useful for editing your app configuration and API credentials during development.\n\nIt works like (and is inspired by) [dotenv](https://github.com/bkeepers/dotenv) (Ruby), [dotenv](https://github.com/motdotla/dotenv) (Node), and [env2](https://github.com/dwyl/env2). It differs from the popular [dotenv](https://github.com/motdotla/dotenv) library for Node in that `nvar` follows Shell syntax (so if you are already loading environment variables via `source`, you can expect this module to work like a drop-in replacement).\n\n\n## Usage\n\nInstall by running:\n\n```sh\nnpm install --save nvar\n```\n\nMake a `.env` file in your app's root folder:\n\n```sh\n# .env (usually added to .gitignore)\nDB_URL='postgresql://user:password@localhost:5432/mydb'\nGITHUB_API_TOKEN=6495e6cf5fb93d68 # quotes are usually optional.\nexport LOGLEVEL=SEVERE # prepend with 'export' (not required for nvar, but typically found in Bash scripts).\n```\n\nThen, require and call `nvar` at the top of your application code:\n\n```js\n// Note the calling brackets at the end.\nrequire('nvar')();\n\n// Variables that were declared in .env in the application's root folder have now been added to process.env.\nconsole.log(process.env.DB_URL); // Prints 'postgresql://user:password@localhost:5432/mydb'.\nconsole.log(process.env.GITHUB_API_TOKEN); // Prints '6495e6cf5fb93d68'.\nconsole.log(process.env.LOGLEVEL); // Prints 'SEVERE'.\n```\n\nOr, if your `.env` file is somewhere else, then do:\n\n```js\nrequire('nvar')('../somedir/my-env.sh')\n```\n\nOr, if you need to change some other options from the defaults, then do:\n\n```js\nrequire('nvar')({\n  // All options listed.\n  path: '../somedir/set-env.sh', // Filepath to envfile\n  source: 'FOO=BAR', // Alternatively, provide the envfile source directly.\n  target: module.exports, // Assign to something else besides process.env instead.\n  enoent: 'warn', // What should happen if the envfile was not found? Set to null|'warn'|'error'.\n  override: 'all' // Whether to override pre-existing variables. Set to 'all'|'empty'|'none'.\n});\n```\n\n## Writing your envfile\n\n**The TL;DR version:** Write lines of `KEY='VALUE'`. No spaces before/after the `=`. Single-quote your values (and if you need a single-quote literal, escape with `'\\''`).\n\n```sh\nFOO='bar'\nENVIRONMENT='development'\nAPI_TOKEN='12345abc'\nGREETING='What'\\''s your name?'\n```\n\n**The in-depth version:** The following shell-isms are supported, so its very likely that you can use `nvar` to read your existing Bash-`source`d envfile, and vice versa.\n\n```sh\nFOO=bar\nEGGS=halfboiled TOAST=kaya # Multiple assignments on the same line work.\nLUNCH=noodle echo ignored # Disregards shell commands.\n\n# Backslash followed by newline breaks the value across multiple lines.\npi=3.141\\\n59265359\n\n# Prepend with 'export', nvar doesn't mind.\nexport gum=secretly\nexport buns=quietly\n# If all your variables are prepended with 'export', then `source`ing your envfile vs. using nvar would do the same thing, so that's convenient.\n\n# Things like '\\', '$', and whitespace do special things in shell. To prevent, a safe choice is single-quotes, which literalizes almost everything.\nstatement='everything i$ literal,\nincluding newlines,\nso that'\\''s okay.' # Escape single-quotes by writing '\\''.\n\n# Double quotes: Mostly like single-quotes, though parameter expansion still works. See further below.\nVERDICT=\"we've decided\nthat this is pretty !@#\\$-ing \\\"cool\\\".\" # Escape \", $, and \\ with a backslash.\n\n# Concatenation\nFILLERS=\"foo\"bar'baz' # Sets a value of 'foobarbaz' (FYI: concatenation is really why '\\'' works as an escape when single-quoting).\n\n# Parameter expansion of prior variables.\nDB_USER=alice\nDB_PASS=in\nDB_HOST=data.land\nDB_PORT=5432\nDB_NAME=fun\nDB_URL=\"${DB_USER}:${DB_PASS}@$DB_HOST:$DB_PORT/${DB_NAME}\" # Curly braces are optional. Can be done within double quotes, or unquoted.\n```\n\nFeel free to review the [test results](https://github.com/sneakertack/nvar/blob/master/tests/results.txt), which also doubles as a specification for the syntax that can be accepted by the module.\n\n## API\n\nHere is a list of options you can pass in as an options object to `nvar`:\n\n### Options\n\nOption | Default | Description\n--- | --- | ---\n`path`\u0026nbsp;\u003csup\u003ev1.0\u003c/sup\u003e | `'./.env'` |  Location of the envfile to load. If you only want to change this filepath, you can pass it directly as a string argument, instead of wrapping it in an options object.\n`source`\u0026nbsp;\u003csup\u003ev1.0\u003c/sup\u003e | `null` | Alternatively, pass in the assignments directly as text, e.g. `'EGGS=halfboiled\\nTOAST=kaya'`. `path` is ignored if `source` is set.\n`target`\u0026nbsp;\u003csup\u003ev1.0\u003c/sup\u003e | `process.env` | Where to save the assignments to.\n`enoent`\u0026nbsp;\u003csup\u003ev1.1\u003c/sup\u003e | `'warn'` if relying on default `path`, `'error'` if path was specified | Whether to throw an error, log a warning to stderr, or do nothing if the file was not found. Irrelevant if using `source` instead of `path`.\n`override`\u0026nbsp;\u003csup\u003ev1.3\u003c/sup\u003e | `'all'` | If a variable already exists in the environment, should `nvar` override it? `'all'` means the environment can be overriden (default). `'empty'` means only empty `''` or unset variables can be set. `'none'` means only unset variables can be set.\u003cbr/\u003e\u003cbr/\u003e\u003csmall\u003e_Advanced needs: Need even more control? Pass in a custom function (params `(key, env)`) that returns `true` or `false` instead. E.g. setting `override` to `(key) =\u003e !/[A-Z]/.test(key)` overrides variables written in lowercase only._\u003c/small\u003e\n\n## Contributing\n\nWhere possible, this module tries to support all shell syntax that might reasonably be expected to appear in a config file. If you believe you have a use case that is not covered, feel free to [raise an issue](https://github.com/sneakertack/nvar/issues).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsneakertack%2Fnvar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsneakertack%2Fnvar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsneakertack%2Fnvar/lists"}