3

I'm using a project with react-redux-starter-kit module, I installed geoip-lite module, and when I imported it, the errors said in the title appeared.

ERROR in ./~/geoip-lite/lib/geoip.js
Module not found: Error: Cannot resolve module 'fs' in <path>/node_modules/geoip-lite/lib
 @ ./~/geoip-lite/lib/geoip.js 1:9-22

ERROR in ./~/geoip-lite/lib/fsWatcher.js
Module not found: Error: Cannot resolve module 'fs' in <path>/node_modules/geoip-lite/lib
 @ ./~/geoip-lite/lib/fsWatcher.js 1:9-22

In webpack.config.js file, I've already tried to modify the target to node instead of web, which is the default, but then the warning below appeared and the website running locally got all blank.

WARNING in ./~/encoding/lib/iconv-loader.js
Critical dependencies:
9:12-34 the request of a dependency is an expression
 @ ./~/encoding/lib/iconv-loader.js 9:12-34

I've also added node: { fs: 'empty' } in the file, but then, this error in the screen:

TypeError: fs.openSync is not a function

I tried to install fs within the module and also download fs-extra and change all occurrences of fs to fs-extra, but no success at all. What can be done?

  • Are you using webpack in order to run bundled code in a browser? Because there is no full featured `fs` module that can run in the browser. – jfriend00 Mar 09 '17 at 06:33
  • I am. I did see about this already, but doing the suggestions lead me to more issues. –  Mar 09 '17 at 07:24
  • You simply can't use the `fs` module or any other module that depends upon it in a browser. The browser does not have a substitute for that functionality. Neither webPack or browserify can magically give the browser file access powers (or any other native code capabilities) it does not have built in. – jfriend00 Mar 09 '17 at 07:31
  • Why is there such module then? Is there any alternative? –  Mar 14 '17 at 07:30
  • The `fs` module exists for running code with node.js and is a built-in part of node.js. If you're trying to run code in a different environment like the browser, then only a subset of the modules are applicable. So, when finding modules in NPM (you apparently found the geoip-lite module, then you have to find modules that are compatible with the browser environment. – jfriend00 Mar 14 '17 at 16:42

1 Answers1

0

Try to add target: 'node' in webpack config section, like this :

var webpackConfig = Object.assign(
  {
    target: 'node',
    ....
};