Skip to content

Commit 0ab684d

Browse files
committed
Update README
1 parent bf7e626 commit 0ab684d

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

README.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,19 +525,43 @@ There are two alternatives to this approach:
525525
1. Preload dotenv: `node --require dotenv/config index.js` (_Note: you do not need to `import` dotenv with this approach_)
526526
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)
527527

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

530-
You are using dotenv on the front-end. Webpack < 5 used to include polyfills for core Node.js modules like `crypto`, `fs`, `os`, and `path`. It doesn't any longer, so these days you need to install and configure a polyfill for it.
530+
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:
531531

532532
```bash
533-
npm install crypto-browserify
533+
npm install node-polyfill-webpack-plugin
534534
```
535535

536-
And then configure it in your webpack config.
536+
Configure your `webpack.config.js` to something like the following.
537537

538538
```js
539-
resolve.fallback: { "crypto": require.resolve("crypto-browserify") }
540-
```
539+
require('dotenv').config()
540+
541+
const path = require('path');
542+
const webpack = require('webpack')
543+
544+
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
545+
546+
module.exports = {
547+
mode: 'development',
548+
entry: './src/index.ts',
549+
output: {
550+
filename: 'bundle.js',
551+
path: path.resolve(__dirname, 'dist'),
552+
},
553+
plugins: [
554+
new NodePolyfillPlugin(),
555+
new webpack.DefinePlugin({
556+
'process.env': {
557+
HELLO: JSON.stringify(process.env.HELLO)
558+
}
559+
}),
560+
]
561+
};
562+
```
563+
564+
Alternatively, just use [dotenv-webpack](https://github.com/mrsteele/dotenv-webpack) which does this and more behind the scenes for you.
541565

542566
### What about variable expansion?
543567

0 commit comments

Comments
 (0)