Search code examples
webpackgun

Webpack, gun, cannot resolve files, etc, gun require cannot resolve module 'fs'


I'm working on a electron app with gundb. After getting everything else working I did a npm install --save gun. It completed with this warn:

npm WARN optional Skipping failed optional dependency /chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.0.14

When I try to:

require('gun');
var endpoints;
var gun = Gun(endpoints);

I get a very long list of errors I can't make since of. They start with:

.../node_modules/fs doesn't exist
.../node_modules/fs.webpack.js doesn't exist
.../node_modules/fs.web.js doesn't exist
.../node_modules/fs.js doesn't exist
.../node_modules/fs.json doesn't exist

And the following cannot resolve:

@ ./~/gun/lib/file.js 14:10-23
@ ./~/gun/lib/wsp.js 61:39-52
@ ./~/ws/lib/WebSocketServer.js 15:10-2
@ ./~/options/lib/options.js 6:9-2
@ ./~/aws-sdk/lib/api_loader.js 1:9-22
@ ./~/aws-sdk/lib/services.js 1:9-22

I'm on Linux. Is fsevent a dependency of gun npm?

UPDATE
To remove as many other variables as possible, I reduced my package.json file down to only electron....eliminating possible issues with webpack, and other dependencies. I also deleted my node_modules and did a fresh npm install & npm install gun.

That revealed a more useful error:

Uncaught ReferenceError: Gun is not defined                gun.js:1470

Which pointed to:

if(typeof window !== "undefined"){ Gun.request = request }
if(typeof module !== "undefined" && module.exports){ module.exports.request = request }

Solution

  • That was a gun bug and the gun team corrected it this morning. After the error was corrected and I updated gun in my project, I was still left with problems bundling with webpack:

    WARNING in ./~/ws/lib/BufferUtil.js
    Module not found: Error: Cannot resolve module 'bufferutil' in /node_modules/ws/lib
     @ ./~/ws/lib/BufferUtil.js 10:19-40
    
    WARNING in ./~/ws/lib/Validation.js
    Module not found: Error: Cannot resolve module 'utf-8-validate' in /node_modules/ws/lib
     @ ./~/ws/lib/Validation.js 10:19-44
    
    WARNING in ./~/formidable/lib/incoming_form.js
    Critical dependencies:
    1:43-50 require function is used in a way in which dependencies cannot be statically extracted
     @ ./~/formidable/lib/incoming_form.js 1:43-50
    
    WARNING in ./~/formidable/lib/file.js
    Critical dependencies:
    1:43-50 require function is used in a way in which dependencies cannot be statically extracted
     @ ./~/formidable/lib/file.js 1:43-50
    
    WARNING in ./~/formidable/lib/json_parser.js
    Critical dependencies:
    1:43-50 require function is used in a way in which dependencies cannot be statically extracted
     @ ./~/formidable/lib/json_parser.js 1:43-50
    
    WARNING in ./~/formidable/lib/querystring_parser.js
    Critical dependencies:
    1:43-50 require function is used in a way in which dependencies cannot be statically extracted
     @ ./~/formidable/lib/querystring_parser.js 1:43-50
    
    WARNING in ./~/aws-sdk/lib/util.js
    Critical dependencies:
    40:30-45 the request of a dependency is an expression
    43:11-53 the request of a dependency is an expression
     @ ./~/aws-sdk/lib/util.js 40:30-45 43:11-53
    
    WARNING in ./~/aws-sdk/lib/api_loader.js
    Critical dependencies:
    13:15-59 the request of a dependency is an expression
    104:12-46 the request of a dependency is an expression
    108:21-58 the request of a dependency is an expression
    114:18-52 the request of a dependency is an expression
     @ ./~/aws-sdk/lib/api_loader.js 13:15-59 104:12-46 108:21-58 114:18-52
    

    I had to add the following to my webpack.config.js to use gun with webpack:

    var webpack = require('webpack');
    module.exports = {
      devtool: "source-map",
      target: "node", 
    
    ....
    
      module: {
        noParse: [/aws-sdk/],
    
    ....
    
     plugins: [
       new webpack.DefinePlugin({ "global.GENTLY": false })
     ]
    ....
    

    At that point everything was working even though I still had the following errors in bash:

    WARNING in ./~/ws/lib/BufferUtil.js
    Module not found: Error: Cannot resolve module 'bufferutil' in /node_modules/ws/lib
     @ ./~/ws/lib/BufferUtil.js 10:19-40
    
    WARNING in ./~/ws/lib/Validation.js
    Module not found: Error: Cannot resolve module 'utf-8-validate' in /node_modules/ws/lib
     @ ./~/ws/lib/Validation.js 10:19-44