├── .editorconfig ├── .eslintignore ├── .eslintrc.js ├── .github ├── ISSUE_TEMPLATE.md └── workflows │ └── ci.yaml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── commitlint.config.js ├── husky.config.js ├── jest.config.js ├── lib ├── logger.js └── module.js ├── package.json ├── renovate.json ├── test ├── .eslintrc.js ├── basic.test.js ├── env-file-name.test.js ├── fixture │ ├── basic │ │ ├── .env │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ ├── env-file-name │ │ ├── .env │ │ ├── .env.two │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ ├── no-env-file │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ └── path-to-env │ │ ├── .env │ │ ├── alternativeenv │ │ └── .env │ │ ├── nuxt.config.js │ │ └── pages │ │ └── index.vue ├── no-env-file.test.js ├── path-to-env.test.js └── tsconfig.json └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_size = 2 6 | indent_style = space 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | # Common 2 | node_modules 3 | dist 4 | .nuxt 5 | coverage 6 | 7 | # Plugin 8 | lib/plugin.js 9 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: [ 4 | '@nuxtjs' 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | name: CI 3 | 4 | on: 5 | push: 6 | branches: 7 | - master 8 | pull_request: 9 | branches: 10 | - master 11 | 12 | jobs: 13 | build: 14 | name: Lint and test 15 | runs-on: ubuntu-latest 16 | env: 17 | CI: true 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | node-version: [18.x] 22 | steps: 23 | - uses: actions/checkout@v4 24 | - name: Setup node 25 | uses: actions/setup-node@v4 26 | with: 27 | node-version: ${{ matrix.node-version }} 28 | cache: yarn 29 | - name: yarn install 30 | run: yarn 31 | - name: Linting 32 | if: ${{ matrix.node-version == '18.x' }} 33 | run: yarn lint 34 | - name: Unittesting 35 | run: yarn jest 36 | - name: Coverage 37 | run: yarn codecov 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.iml 3 | .idea 4 | *.log* 5 | .nuxt 6 | .vscode 7 | .DS_Store 8 | coverage 9 | dist 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ### [1.4.2](https://github.com/nuxt-community/dotenv-module/compare/v1.4.1...v1.4.2) (2023-12-15) 6 | 7 | 8 | ### Bug Fixes 9 | 10 | * **typo:** removed twicly noted can ([#47](https://github.com/nuxt-community/dotenv-module/issues/47)) ([003d890](https://github.com/nuxt-community/dotenv-module/commit/003d8903d951eb8b41f5a6211ce366c28972c1cb)) 11 | 12 | 13 | ## [1.3.0](https://github.com/nuxt-community/dotenv-module/compare/v1.1.1...v1.3.0) (2018-07-12) 14 | 15 | - add `systemvars` option. Setting this to true will allow your system set variables to work. 16 | 17 | 18 | ## [1.2.1](https://github.com/nuxt-community/dotenv-module/compare/v1.2.0...v1.2.1) (2018-10-13) 19 | 20 | - upgrade dependencies 21 | 22 | 23 | ## [1.2.0](https://github.com/nuxt-community/dotenv-module/compare/v1.1.1...v1.2.0) (2018-10-13) 24 | 25 | - add `filename` option. This options allows you to use an alternative filename to replace `.env` 26 | 27 | 28 | ## [1.1.1](https://github.com/nuxt-community/dotenv-module/compare/v1.1.0...v1.1.1) (2018-03-25) 29 | 30 | - Don't crash when .env file is not present 31 | 32 | 33 | ## [1.1.0](https://github.com/nuxt-community/dotenv-module/compare/v1.0.0...v1.1.0) (2017-10-10) 34 | 35 | - Add `path` option. This option allows you to specify a different folder to load the `.env` from 36 | 37 | 38 | ## [1.0.0](https://github.com/nuxt-community/dotenv-module/compare/v0.0.1...v1.0.0) (2017-09-25) 39 | 40 | - Public release of the package 41 | 42 | 43 | 44 | ## 0.0.1 (2017-09-23) 45 | 46 | - Initial version 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Nuxt Community 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @nuxtjs/dotenv 2 | 3 | [![npm version][npm-version-src]][npm-version-href] 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] 5 | [![Circle CI][circle-ci-src]][circle-ci-href] 6 | [![Codecov][codecov-src]][codecov-href] 7 | [![License][license-src]][license-href] 8 | 9 | > A Nuxt.js module that loads your .env file into your context options 10 | 11 | [📖 **Release Notes**](./CHANGELOG.md) 12 | 13 | :warning: With Nuxt v2.13 you might want to migrate from @nuxtjs/dotenv module to use the [new runtime config](https://nuxtjs.org/blog/moving-from-nuxtjs-dotenv-to-runtime-config). 14 | 15 | ## Features 16 | 17 | The module loads variables from your .env file directly into your nuxt.js application `context` and `process.env`. 18 | 19 | ## Setup 20 | 21 | 1. Add `@nuxtjs/dotenv` dependency to your project 22 | 23 | ```bash 24 | yarn add --dev @nuxtjs/dotenv # or npm install --save-dev @nuxtjs/dotenv 25 | ``` 26 | 27 | 2. Add `@nuxtjs/dotenv` to the `buildModules` section of `nuxt.config.js` 28 | 29 | :warning: If you are using a Nuxt version previous than **v2.9** you have to install module as a `dependency` (No `--dev` or `--save-dev` flags) and also use `modules` section in `nuxt.config.js` instead of `buildModules`. 30 | 31 | ```js 32 | export default { 33 | buildModules: [ 34 | // Simple usage 35 | '@nuxtjs/dotenv', 36 | 37 | // With options 38 | ['@nuxtjs/dotenv', { /* module options */ }] 39 | ] 40 | } 41 | ``` 42 | 43 | ### Using top level options 44 | 45 | ```js 46 | export default { 47 | buildModules: [ 48 | '@nuxtjs/dotenv' 49 | ], 50 | dotenv: { 51 | /* module options */ 52 | } 53 | } 54 | ``` 55 | 56 | ## Options 57 | 58 | ### `only` 59 | 60 | - Type: `Array[String]` 61 | - Default: `null` 62 | 63 | If you want to restrict what's accessible into the context, 64 | you can pass to the module options an `only` array with the keys you want to allow. 65 | 66 | ```js 67 | export default { 68 | buildModules: [ 69 | ['@nuxtjs/dotenv', { only: ['some_key'] }] 70 | ] 71 | } 72 | ``` 73 | 74 | ### `path` 75 | 76 | - Type: `String` 77 | - Default: `srcDir` 78 | 79 | By default, the we'll be loading the `.env` file from the root of your project. 80 | If you want to change the path of the folder where we can find the `.env` file, then use the `path` option. 81 | 82 | ```js 83 | export default { 84 | buildModules: [ 85 | ['@nuxtjs/dotenv', { path: '/path/to/my/global/env/' }] 86 | ] 87 | } 88 | ``` 89 | 90 | > **Note:** that this is the path to the *folder* where the `.env` file live, not to the `.env` file itself. 91 | 92 | The path can be absolute or relative. 93 | 94 | ### systemvars 95 | 96 | - Type: `Boolean` 97 | - Default: `false` 98 | 99 | By default this is false and variables from your system will be ignored. 100 | Setting this to true will allow your system set variables to work. 101 | 102 | ```js 103 | export default { 104 | buildModules: [ 105 | ['@nuxtjs/dotenv', { systemvars: true }] 106 | ] 107 | } 108 | ``` 109 | 110 | ### filename 111 | 112 | - Type: `String` 113 | - Default: `.env` 114 | 115 | We can override the filename when we need to use different config files for different environments. 116 | 117 | ```js 118 | export default { 119 | buildModules: [ 120 | ['@nuxtjs/dotenv', { filename: '.env.prod' }] 121 | ] 122 | } 123 | ``` 124 | 125 | ## Usage 126 | 127 | After creating your .env file in the project root, simply run your usual `yarn dev` or `npm run dev`. 128 | The variable inside the .env file will be added to the context (`context.env`) and process (`process.env`). 129 | 130 | ## Using .env file in nuxt.config.js 131 | 132 | This module won't overload the environment variables of the process running your build. 133 | 134 | If you need to use variables from your .env file at this moment, 135 | just prepend `require('dotenv').config()` to your `nuxt.config.js`: 136 | 137 | ```js 138 | require('dotenv').config() 139 | 140 | export default { 141 | // your usual nuxt config. 142 | } 143 | ``` 144 | 145 | This will works thanks to the `dotenv` library provided by this module as a dependency. 146 | If you decided to ignore some values from your `.env` file in the module configuration, this won't apply here. 147 | 148 | ## License 149 | 150 | [MIT License](./LICENSE) 151 | 152 | Copyright (c) Nuxt Community 153 | 154 | 155 | [npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/dotenv/latest.svg?style=flat-square 156 | [npm-version-href]: https://npmjs.com/package/@nuxtjs/dotenv 157 | 158 | [npm-downloads-src]: https://img.shields.io/npm/dt/@nuxtjs/dotenv.svg?style=flat-square 159 | [npm-downloads-href]: https://npmjs.com/package/@nuxtjs/dotenv 160 | 161 | [circle-ci-src]: https://img.shields.io/circleci/project/github/nuxt-community/dotenv-module.svg?style=flat-square 162 | [circle-ci-href]: https://circleci.com/gh/nuxt-community/dotenv-module 163 | 164 | [codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/dotenv-module.svg?style=flat-square 165 | [codecov-href]: https://codecov.io/gh/nuxt-community/dotenv-module 166 | 167 | [license-src]: https://img.shields.io/npm/l/@nuxtjs/dotenv.svg?style=flat-square 168 | [license-href]: https://npmjs.com/package/@nuxtjs/dotenv 169 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ 3 | '@commitlint/config-conventional' 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /husky.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | hooks: { 3 | 'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS', 4 | 'pre-commit': 'yarn lint', 5 | 'pre-push': 'yarn lint' 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | collectCoverage: true, 3 | collectCoverageFrom: ['lib/**/*.js', '!lib/plugin.js'], 4 | testEnvironment: 'node' 5 | } 6 | -------------------------------------------------------------------------------- /lib/logger.js: -------------------------------------------------------------------------------- 1 | const consola = require('consola') 2 | 3 | module.exports = consola.withTag('nuxt:dotenv') 4 | -------------------------------------------------------------------------------- /lib/module.js: -------------------------------------------------------------------------------- 1 | const { readFileSync, accessSync, constants } = require('fs') 2 | const { join } = require('path') 3 | const { parse } = require('dotenv') 4 | const logger = require('./logger') 5 | 6 | module.exports = function (moduleOptions) { 7 | const options = { 8 | only: null, 9 | path: this.options.srcDir, 10 | filename: '.env', 11 | systemvars: false, 12 | ...this.options.dotenv, 13 | ...moduleOptions 14 | } 15 | 16 | const envFilePath = join(options.path, options.filename) 17 | 18 | try { 19 | accessSync(envFilePath, constants.R_OK) 20 | } catch (err) { 21 | logger.warn(`No \`${options.filename}\` file found in \`${options.path}\`.`) 22 | return 23 | } 24 | 25 | const envConfig = parse(readFileSync(envFilePath)) 26 | 27 | if (options.systemvars) { 28 | Object.keys(process.env).forEach((key) => { 29 | if (!(key in envConfig)) { 30 | envConfig[key] = process.env[key] 31 | } 32 | }) 33 | } 34 | 35 | Object.keys(envConfig).forEach((key) => { 36 | if (!Array.isArray(options.only) || options.only.includes(key)) { 37 | this.options.env[key] = this.options.env[key] || envConfig[key] 38 | } 39 | }) 40 | } 41 | 42 | module.exports.meta = require('../package.json') 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@nuxtjs/dotenv", 3 | "version": "1.4.2", 4 | "description": "A Nuxt.js module that loads your .env file into your context options", 5 | "repository": "nuxt-community/dotenv-module", 6 | "license": "MIT", 7 | "contributors": [ 8 | "Julien Tant " 9 | ], 10 | "files": [ 11 | "lib" 12 | ], 13 | "main": "lib/module.js", 14 | "scripts": { 15 | "lint": "eslint --ext .js,.vue lib test", 16 | "release": "yarn test && standard-version && git push --follow-tags && npm publish", 17 | "test": "yarn lint && jest" 18 | }, 19 | "dependencies": { 20 | "consola": "^3.2.3", 21 | "dotenv": "^8.2.0" 22 | }, 23 | "devDependencies": { 24 | "@commitlint/cli": "^18.4.2", 25 | "@commitlint/config-conventional": "^18.4.2", 26 | "@nuxtjs/eslint-config": "^12.0.0", 27 | "@nuxtjs/module-test-utils": "^1.6.3", 28 | "@types/jest": "^29.5.8", 29 | "codecov": "^3.8.3", 30 | "eslint": "^8.53.0", 31 | "eslint-plugin-jest": "^27.6.0", 32 | "husky": "^8.0.3", 33 | "jest": "^29.7.0", 34 | "nuxt-edge": "^2.17.2-28284499.5674f49", 35 | "standard-version": "^9.5.0" 36 | }, 37 | "publishConfig": { 38 | "access": "public" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "@nuxtjs" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /test/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: ['plugin:jest/recommended'] 3 | } 4 | -------------------------------------------------------------------------------- /test/basic.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('basic', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = await setup(loadConfig(__dirname, 'basic'))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('variables from the .env file should have been loaded', async () => { 15 | const html = await get('/') 16 | expect(html).toContain('foo: bar') 17 | expect(html).toContain('bar: baz') 18 | expect(html).toContain('baz: foo') 19 | }) 20 | 21 | test('variables from the .env file should NOT override existing values from nuxt.config.js', async () => { 22 | const html = await get('/') 23 | expect(html).not.toContain('foo: baz') 24 | }) 25 | 26 | test('when an only array is passed, the variables not included should not be loaded', async () => { 27 | const html = await get('/') 28 | expect(html).toContain('more1: yep') 29 | expect(html).not.toContain('more2: nope') 30 | }) 31 | }) 32 | -------------------------------------------------------------------------------- /test/env-file-name.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('.env file name', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = await setup(loadConfig(__dirname, 'env-file-name'))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('variables from .env.two file should have been loaded', async () => { 15 | const html = await get('/') 16 | expect(html).toContain('foo: bar2') 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /test/fixture/basic/.env: -------------------------------------------------------------------------------- 1 | baz=foo 2 | foo=baz 3 | more1=yep 4 | more2=nope 5 | NODE_ENV=test 6 | -------------------------------------------------------------------------------- /test/fixture/basic/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | dev: false, 3 | rootDir: __dirname, 4 | render: { 5 | resourceHints: false 6 | }, 7 | env: { 8 | foo: 'bar', 9 | bar: 'baz' 10 | }, 11 | buildModules: [ 12 | { handler: require('../../../') } 13 | ], 14 | dotenv: { 15 | only: ['baz', 'foo', 'more1'], 16 | systemvars: true 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test/fixture/basic/pages/index.vue: -------------------------------------------------------------------------------- 1 | 10 | 11 | 28 | -------------------------------------------------------------------------------- /test/fixture/env-file-name/.env: -------------------------------------------------------------------------------- 1 | foo=bar1 2 | -------------------------------------------------------------------------------- /test/fixture/env-file-name/.env.two: -------------------------------------------------------------------------------- 1 | foo=bar2 2 | -------------------------------------------------------------------------------- /test/fixture/env-file-name/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | dev: false, 3 | rootDir: __dirname, 4 | render: { 5 | resourceHints: false 6 | }, 7 | buildModules: [ 8 | { handler: require('../../../') } 9 | ], 10 | dotenv: { 11 | filename: '.env.two' 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/fixture/env-file-name/pages/index.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 16 | -------------------------------------------------------------------------------- /test/fixture/no-env-file/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | dev: false, 3 | rootDir: __dirname, 4 | render: { 5 | resourceHints: false 6 | }, 7 | buildModules: [ 8 | { handler: require('../../../') } 9 | ], 10 | env: { 11 | foo: 'oof' 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/fixture/no-env-file/pages/index.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 16 | -------------------------------------------------------------------------------- /test/fixture/path-to-env/.env: -------------------------------------------------------------------------------- 1 | foo=bar -------------------------------------------------------------------------------- /test/fixture/path-to-env/alternativeenv/.env: -------------------------------------------------------------------------------- 1 | foo=baz -------------------------------------------------------------------------------- /test/fixture/path-to-env/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | dev: false, 3 | rootDir: __dirname, 4 | render: { 5 | resourceHints: false 6 | }, 7 | buildModules: [ 8 | { handler: require('../../../') } 9 | ], 10 | dotenv: { 11 | path: './test/fixture/path-to-env/alternativeenv' 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/fixture/path-to-env/pages/index.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 16 | -------------------------------------------------------------------------------- /test/no-env-file.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get } = require('@nuxtjs/module-test-utils') 2 | const logger = require('../lib/logger') 3 | 4 | logger.mockTypes(() => jest.fn()) 5 | 6 | describe('no .env file', () => { 7 | let nuxt 8 | 9 | beforeAll(async () => { 10 | ({ nuxt } = await setup(loadConfig(__dirname, 'no-env-file'))) 11 | }, 60000) 12 | 13 | afterAll(async () => { 14 | await nuxt.close() 15 | }) 16 | 17 | test('should warn if not found the .env file', () => { 18 | expect(logger.warn).toHaveBeenCalledWith(expect.stringMatching(/^No `(.*)` file found in `(.*)`.$/)) 19 | }) 20 | 21 | test('when no .env file, variable from system env should be loaded', async () => { 22 | const html = await get('/') 23 | expect(html).toContain('foo: oof') 24 | }) 25 | }) 26 | -------------------------------------------------------------------------------- /test/path-to-env.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('path to .env', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = await setup(loadConfig(__dirname, 'path-to-env'))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('variables from the alternative .env file should have been loaded', async () => { 15 | const html = await get('/') 16 | expect(html).toContain('foo: baz') 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /test/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": true, 4 | "checkJs": true, 5 | "module": "commonjs", 6 | "types": [ 7 | "jest" 8 | ] 9 | }, 10 | } 11 | --------------------------------------------------------------------------------