Skip to content

Commit

Permalink
Merge pull request #745 from motdotla/browser-defaults
Browse files Browse the repository at this point in the history
Remove browser key except for `fs`
  • Loading branch information
motdotla committed May 31, 2023
2 parents 3f40e12 + 080779a commit 1ab96d7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. See [standa

## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.1.0...master)

## [16.1.3](https://github.com/motdotla/dotenv/compare/v16.1.2...v16.1.3) (2023-05-31)

### Removed

- Removed `browser` keys for `path`, `os`, and `crypto` in package.json. These were set to false incorrectly as of 16.1. Instead, if using dotenv on the front-end make sure to include polyfills for `path`, `os`, and `crypto`. [node-polyfill-webpack-plugin](https://github.com/Richienb/node-polyfill-webpack-plugin) provides these.

## [16.1.2](https://github.com/motdotla/dotenv/compare/v16.1.1...v16.1.2) (2023-05-31)

### Changed
Expand Down
38 changes: 38 additions & 0 deletions README.md
Expand Up @@ -525,6 +525,44 @@ There are two alternatives to this approach:
1. Preload dotenv: `node --require dotenv/config index.js` (_Note: you do not need to `import` dotenv with this approach_)
2. Create a separate file that will execute `config` first as outlined in [this comment on #133](https://github.com/motdotla/dotenv/issues/133#issuecomment-255298822)

### Why am I getting the error `Module not found: Error: Can't resolve 'crypto|os|path'`?

You are using dotenv on the front-end and have not included a polyfill. Webpack < 5 used to include these for you. Do the following:

```bash
npm install node-polyfill-webpack-plugin
```

Configure your `webpack.config.js` to something like the following.

```js
require('dotenv').config()

const path = require('path');
const webpack = require('webpack')

const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')

module.exports = {
mode: 'development',
entry: './src/index.ts',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new NodePolyfillPlugin(),
new webpack.DefinePlugin({
'process.env': {
HELLO: JSON.stringify(process.env.HELLO)
}
}),
]
};
```

Alternatively, just use [dotenv-webpack](https://github.com/mrsteele/dotenv-webpack) which does this and more behind the scenes for you.

### What about variable expansion?

Try [dotenv-expand](https://github.com/motdotla/dotenv-expand)
Expand Down
9 changes: 3 additions & 6 deletions package.json
Expand Up @@ -55,13 +55,10 @@
"tar": "^6.1.11",
"typescript": "^4.8.4"
},
"browser": {
"fs": false,
"path": false,
"os": false,
"crypto": false
},
"engines": {
"node": ">=12"
},
"browser": {
"fs": false
}
}

0 comments on commit 1ab96d7

Please sign in to comment.