Ws: Cannot find module "fs"

Created on 9 Jan 2016  路  9Comments  路  Source: websockets/ws

Hello,

Firstly, I'm sorry if this isn't an issue related to the ws library - but I had to start somewhere.

I'm currently working on a project with Vue.js - my current code looks like this.

import Vue from 'vue';

new Vue({

    ready: function() {

        this.connect();

    },

    data: function() {
        return {
            socket: null
        }
    },

    methods: {

        /**
         * Connect to the Pushover WebSocket in order to
         * retrieve push notifications.
         * 
         * @return mixed
         */
        connect: function() {

            var WebSocket = require('ws');

            this.socket = new WebSocket('wss://client.pushover.net/push:443');

            this.socket.on('open', function() {
                this.socket.send('login:' + localStorage.getItem('device_id'));
            });

            this.socket.on('message', function(data) {
                console.log(data);

                alert('got a message');
            });

        }

    },

});

When the connect method is called, I get the following error in my console.

Uncaught Error: Cannot find module "fs"

Do you have any idea why I might be receiving this? I know everything but the code within my connect method is ok as I've tested this.

I hope you can help!

Most helpful comment

Similar error is https://github.com/webpack/react-starter/issues/3#issuecomment-53395089

When developing isomorphic javascript app, the packages we requireed should have browser field in package.json to tell, for example webpack, to exclude server-side module. I think ws would be nice to add this to package.json:

     "tinycolor": "0.0.x",
     "utf-8-validate": "1.2.x"
   },
+  "browser": {
+    "fs": false,
+    "tls": false
+  },
   "gypfile": true
 }

Otherwise, we could add this to webpack.config.js

         loaders: ['style', 'raw']
       }
     ]
+  },
+  node: {
+    fs: 'empty',
+    tls: 'empty'
   }
 };

All 9 comments

I honestly don't know why you are getting this error but it's in no way related to ws. The fs is a core module of node so if that is missing there is something seriously wrong with either your installation or how you are using the code.

Only happens when I use this library though - hence the reason for the issue. I'll continue to investigate elsewhere :)

@JoeDawson Experiencing the same issue here, have you the found the reason?

@hrmoller unfortunately ended up abandoning the library and using navtive WebSockets in the end. Bit of a shame, but I think it was related to my webpack config somehow if that helps point you in the right direction?

Before this one there's another error:

ERROR in /.../~/ws/lib/WebSocketServer.js
Module not found: Error: Cannot resolve module 'tls' in /.../node_modules/ws/lib
 @ /.../~/ws/lib/WebSocketServer.js 15:10-24

ERROR in /.../~/options/lib/options.js
Module not found: Error: Cannot resolve module 'fs' in /.../node_modules/options/lib
 @ /.../~/options/lib/options.js 6:9-22

So the issue is related to the fact that Webpack cannot require tls and fs (the latter comes from a dependency of bufferutil, which is also required in ws), which are nodejs specific.

The problem doesn't appear in 0.8.x as there's a fallback for browser in package.json:

"browser": "./lib/browser.js"

Incompatibility with webpack should be in README.
That would have saved me half a day before going to native ws.

Similar error is https://github.com/webpack/react-starter/issues/3#issuecomment-53395089

When developing isomorphic javascript app, the packages we requireed should have browser field in package.json to tell, for example webpack, to exclude server-side module. I think ws would be nice to add this to package.json:

     "tinycolor": "0.0.x",
     "utf-8-validate": "1.2.x"
   },
+  "browser": {
+    "fs": false,
+    "tls": false
+  },
   "gypfile": true
 }

Otherwise, we could add this to webpack.config.js

         loaders: ['style', 'raw']
       }
     ]
+  },
+  node: {
+    fs: 'empty',
+    tls: 'empty'
   }
 };

I'm new to ws,and I just got the same problem today.
Then I follow the solution as @xxd3vin says,however, I got an new problem as
http://stackoverflow.com/questions/29829597/i-get-a-status-200-when-connecting-to-the-websocket-but-it-is-an-error# mentioned.
Thus, I realized there is something seriously wrong with how we are using the code.

In the browser, you are using the native WebSocket class and not SockJS !

So, we should just remove var WebSocket = require('ws'); and change the url to var ws = new WebSocket(yourhost+'/websocket') in the client side js. Finally, solve the problem perfectly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pmcalabrese picture pmcalabrese  路  4Comments

anonaka picture anonaka  路  5Comments

makc picture makc  路  4Comments

Globik picture Globik  路  3Comments

nodesocket picture nodesocket  路  4Comments