sql.js icon indicating copy to clipboard operation
sql.js copied to clipboard

Error: Cannot find module 'ws'

Open steventen opened this issue 9 years ago • 12 comments

Hi, I'm using npm to install sql.js. But when watchify try to bundle the app up, it will throw this error:

 Error: Cannot find module 'ws' from 'C:\my_location\node_modules\sql.js\js'

And in the js file, I only have something like this:

var fs = require('fs');
var SQL = require('sql.js');

var filebuffer = fs.readFileSync('../../db/my_db.sqlite');
var _sqlConn = new SQL.Database(filebuffer);

I'm using a Windows 7 laptop

I really don't know how can i solve this, thanks

steventen avatar Jun 26 '15 15:06 steventen

I don't think there is any reference anywhere in the code to the ws module. Are you sure this issue is related to sql.js?

lovasoa avatar Jun 26 '15 20:06 lovasoa

I also have the same issue, right after installing sql.js

I found a require('ws') in sql.js It is minified but in sublime text i found it line 336

d=new (s?require("ws"):window.WebSocket) Error("WebSocket URL must be in the format ws(s)://address:port"))

moimael avatar Jul 09 '15 21:07 moimael

Relevant issue : https://github.com/fabiosantoscode/require-emscripten/issues/1

moimael avatar Jul 09 '15 21:07 moimael

I'm having this issue as well.

nathanhere avatar Aug 21 '15 04:08 nathanhere

I'm using webpack to build my app and I've got the same:

ERROR in ./~/sql.js/js/sql.js
Module not found: Error: Cannot resolve module 'fs' in /Users/thai/Documents/CrossMIP/frame/node_modules/sql.js/js
 @ ./~/sql.js/js/sql.js 4:141-154 361:205-218

ERROR in ./~/sql.js/js/sql.js
Module not found: Error: Cannot resolve module 'ws' in /Users/thai/Documents/CrossMIP/frame/node_modules/sql.js/js
 @ ./~/sql.js/js/sql.js 337:113-126 342:44-57```

chauthai avatar Sep 02 '15 12:09 chauthai

I've fixed the problem by including the sql.js lib manually in my index.html and bypassing webpack or browserify build steps. Works fine so far!

chauthai avatar Sep 03 '15 14:09 chauthai

Same here with SystemJS and jspm. See also https://github.com/jspm/jspm-cli/issues/1108

screendriver avatar Sep 09 '15 10:09 screendriver

Ok, the problem with the tools you use is that they look at the code for uses of require() and try to include the dependencies. The tool with which sql.js is compiled (emscripten) generates conditional requires, that won't be used in the browser. So either file an issue to the tool you use to process your js for it to be more clever about requires, or to emscripten not to generate these requires. But anyway, I don't think there is something I can do in sql.js.

lovasoa avatar Sep 09 '15 10:09 lovasoa

@kripken : Any idea about this?

lovasoa avatar Sep 09 '15 10:09 lovasoa

Emscripten generates those conditional requires so the same output can run in node and on the web (and elsewhere). We've discussed adding compile-time options to target only one platform, but no one has written a pull request yet.

kripken avatar Sep 09 '15 20:09 kripken

Here is another fix for the Error: Cannot resolve module 'fs' error reported by @chauthai - use an externals block in your webpack config to make webpack think the fs module is external like this:

    externals: [
        {fs: true}  // to get sql.js to load - emscripten uses the fs module in certain codepaths we don't use so fake its availability
    ],

dougmartin avatar Mar 16 '17 12:03 dougmartin

This is my way, to use webpack with sql.js

The line below is added in html file, It seems that we can't build sql.js by webpack.

skysign avatar Oct 21 '17 08:10 skysign