Next.js: fs missing from webpack

Created on 9 Aug 2017  路  8Comments  路  Source: vercel/next.js

getting this error

 ERROR  Failed to compile with 1 errors                                                                21:03:36

This module was not found:

* fs in ./~/request/lib/har.js

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

from googling around, it seems some people had success with adding

node: {
  fs: "empty"
}

to their webpack config

I tried adding that in next.config.js without success.
could it be syntax, or is that not the correct file ?

similar issue seen here https://github.com/josephsavona/valuable/issues/9

Most helpful comment

This can be solved with adding config.node = {fs: "empty"}; to your config.

All 8 comments

Your imported modules must be universal/isomorphic compatible, that means they can't use the fs module and other non-universal modules (net, http, etc.).

Like Sergio said, it's not possible to use fs.

This can be solved with adding config.node = {fs: "empty"}; to your config.

I've added the node property to the config but this still isn't working for me.

My next.config.js looks like this...

const {PHASE_DEVELOPMENT_SERVER} = require('next/constants')
module.exports = (phase, {defaultConfig}) => {
const myConfig = {
publicRuntimeConfig: {
...
},
webpack: (config, {buildId, dev, isServer, defaultLoaders}) => {
config.node = {
fs: "empty"
};
return config;
}
}

if (phase === PHASE_DEVELOPMENT_SERVER) {
const publicRuntimeConfig = Object.assign(myConfig.publicRuntimeConfig, {
...
});
return Object.assign(myConfig, {
publicRuntimeConfig
});
}
else {
return myConfig;
}
}

What am I doing wrong?

you can also just exclude index.js

Good point from @billnbell. Just import for example MapboxDraw like this:
import MapboxDraw from "@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw";

Thank you @RobinDvorak!!

For Vue users. His solution also works in the nuxt.config.js

// next.config.js

module.exports = withImages(
  withCSS(
    withSass({
      exportPathMap: function() {
        return {
          '/': { page: '/' }
        }
      },
      webpack(config) {
        config.node = { fs: 'empty' }
        return config
      },
    })
  )
)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

renatorib picture renatorib  路  3Comments

pie6k picture pie6k  路  3Comments

knipferrc picture knipferrc  路  3Comments

ghost picture ghost  路  3Comments

YarivGilad picture YarivGilad  路  3Comments