Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module "fs" #662

Closed
joedawson opened this issue Jan 9, 2016 · 9 comments
Closed

Cannot find module "fs" #662

joedawson opened this issue Jan 9, 2016 · 9 comments

Comments

@joedawson
Copy link

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!

@3rd-Eden
Copy link
Member

3rd-Eden commented Jan 9, 2016

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.

@3rd-Eden 3rd-Eden closed this as completed Jan 9, 2016
@joedawson
Copy link
Author

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

@hrmoller
Copy link

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

@joedawson
Copy link
Author

joedawson commented Jan 13, 2016

@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?

@zalmoxisus
Copy link

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.

@zalmoxisus
Copy link

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

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

@ausrasul
Copy link

ausrasul commented Jul 1, 2016

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

@xx7y7xx
Copy link

xx7y7xx commented Sep 2, 2016

Similar error is webpack/react-starter#3 (comment)

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'
   }
 };

@Vastness
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants