dotenv icon indicating copy to clipboard operation
dotenv copied to clipboard

Can't resolve 'fs'

Open sourivore opened this issue 9 years ago • 6 comments

When I load dotenv without webpack, I have the following message : Module not found: Error: Can't resolve 'fs' in '[MYPATH]\node_modules\dotenv\lib' ModuleNotFoundError: Module not found: Error: Can't resolve 'fs' in '[MYPATH]\node_modules\dotenv\lib'

I don't use webpack and can't change webpack config

sourivore avatar Oct 24 '17 13:10 sourivore

The fs module is core to Node.js which is the intended environment for dotenv. If you want to manage environment variables on the client, I would suggest using webpack's built-in replacement feature or just if/switch statements.

maxbeatty avatar Oct 24 '17 17:10 maxbeatty

Just wanted to share another alternative as mentioned here: https://github.com/webpack-contrib/css-loader/issues/447#issuecomment-285598881

Add the following to your Webpack config:

node: {
   fs: "empty"
}

david-potgieter avatar Jan 12 '19 09:01 david-potgieter

This makes the job as well.

const webpackConfig = { target: 'node' };

module.exports = webpackConfig; // Export all custom Webpack configs.

danieldanielecki avatar Aug 15 '19 08:08 danieldanielecki

As mentioned above, dotenv is really only intended for the server.

If you're trying to work w/ the client, and you're using webpack, you can leverage the dotenv-webpack module. I've found it works pretty well.

nathguen avatar May 27 '21 01:05 nathguen

This makes the job as well.

const webpackConfig = { target: 'node' };

module.exports = webpackConfig; // Export all custom Webpack configs.

how can I fix that on client?

image

tednaaa avatar Sep 17 '21 13:09 tednaaa

I found this solution in stack overflow and it works for me. So, you can give it a shot.

if you encounter this error with a create-react-app app, you need to go to the 'react-scripts' webpack config.

node_modules/react_scripts/config/webpack.config.js

under 'resolve':

resolve: { .... // add the fallback setting below fallback: { "fs": false, "os": false, "path": false }, .... } Share Improve th

Aja-Edward avatar Mar 04 '23 04:03 Aja-Edward