dotenv-module: fs dependency not found

I’ve follow the instructions to add the dotenv module. When I run npm run dev I’m getting the following error:

This dependency was not found:

  • fs in ./node_modules/@nuxtjs/dotenv/dist/index.js

To install it, you can run: npm install --save fs

Digging into this npm package its seems to be a security-holder package. How to solve the error?

<div align="right">This question is available on Nuxt.js community (#c11)</div>

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 19
  • Comments: 28 (3 by maintainers)

Commits related to this issue

Most upvoted comments

@s-ferdie @tobiasfrei

You could try updating node.

I had that issue a while ago. Not in a nuxt project, but in a classic webpack app. I found that trick somewhere on github that worked for me.

So try adding a config.node object inside the build.extend function of your nuxt.config.js :

build: {
    extend (config, { isDev, isClient }) {
 
       config.node = {
            fs: 'empty'
        }
 
       // ....
    }
}

After that if you get the same error, but for other dependencies, like net or tls. You can add them as well in the same config.node

The snippet above is not syntactically correct. To save someone some time, here it is (of course the ... are also not syntactically correct, they’re just there to guide you where you place this):

// nuxt.config.js

export default {

    ...

    build: {

        ...

        extend: function (config, {isDev, isClient}) {

            config.node = {

                fs: "empty"
            };
        }
    },

    ...
};

Just having a similar issue, and solve it by adding config.node = { fs: “empty”} into the build section in nuxt.config.js. Thanks ~ @nicroto, @abernh

   build: {
    /*
    ** You can extend webpack config here
    */

    extend (config, ctx) {
        config.node = {
            fs: "empty"
        };
    }
  }

Seeing this same error with latest Node LTS + Nuxt 2.20 and the above mentioned config change does not fix it.

I had to restart the server after changing the config and it works for me. But, i have a problem calling “fs” functions on one node_modules. “fs.writeFileSync is not a function”

via https://github.com/webpack-contrib/css-loader/issues/447 this seems not to be a nuxt issue but a webpack issue and it is “solved” by adding the “node:fs:empty” solution to the webpack config parameters.

This also means @J0ssue that this issue is solved for gridsome by adding the following to the gridsome.config.js

module.exports = {
  ...
  configureWebpack: {
    node: {
      fs: "empty"
    }
  }

@phikhi thank you so much for helping on this! ❤️

Also this happens if you just try and use dotenv in nuxt.config.js, like:

import { config } from 'dotenv'

const { parsed } = config()

Which is also solved with the “fs empty” fix.

Is this a safe patch? What if some nuxt modules need fs? Is it better to guard this with:

if (!ctx.isServer) config.node.fs = "empty"

Or something similar?

I have the same problem, and fixed it by KentChun33333’s resolution.

The solution by @nicroto above solved it for me.

I solved this because i added dotenv to the devDependencies instead of to the dependencies.