sql-js / sql.js

A javascript library to run SQLite on the web.
http://sql.js.org
Other
12.59k stars 1.06k forks source link

Error: Cannot find module 'ws' #112

Open steventen opened 9 years ago

steventen commented 9 years ago

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

lovasoa commented 9 years ago

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?

moimael commented 9 years ago

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 commented 9 years ago

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

nathanhere commented 9 years ago

I'm having this issue as well.

chauthai commented 9 years ago

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 commented 9 years ago

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!

screendriver commented 9 years ago

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

lovasoa commented 9 years ago

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 commented 9 years ago

@kripken : Any idea about this?

kripken commented 9 years ago

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.

dougmartin commented 7 years ago

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
    ],
skysign commented 6 years ago

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.