Shadow-cljs: Native modules don't work in browser build

Created on 21 Aug 2018  路  3Comments  路  Source: thheller/shadow-cljs

I try to add sqlite3 to electron app. I get this errors on compilation:

[2018-08-21 16:52:32.299 - INFO] :shadow.build.npm/js-invalid-requires - {:resource-name "node_modules/sqlite3/lib/sqlite3.js", :requires [{:line 4, :column 14}]} [2018-08-21 16:52:32.544 - INFO] :shadow.build.npm/js-invalid-requires - {:resource-name "node_modules/node-pre-gyp/lib/node-pre-gyp.js", :requires [{:line 52, :column 13} {:line 184, :column 38}]} [2018-08-21 16:52:33.958 - INFO] :shadow.build.npm/js-invalid-requires - {:resource-name "node_modules/node-pre-gyp/lib/pre-binding.js", :requires [{:line 20, :column 22}]} [2018-08-21 16:52:33.981 - INFO] :shadow.build.npm/js-invalid-requires - {:resource-name "node_modules/node-pre-gyp/lib/util/versioning.js", :requires [{:line 17, :column 20}]}

And this in electron console:

Uncaught Error: browser bootstrap used in incorrect target at Object.<anonymous> (/Users/endenwer/projects/electron-quick-start/cljs_output/main.js:2935:11) at Object.<anonymous> (/Users/endenwer/projects/electron-quick-start/cljs_output/main.js:3050:3) at Object.<anonymous> (/Users/endenwer/projects/electron-quick-start/cljs_output/main.js:3069:3) at Module._compile (module.js:642:30) at Object.Module._extensions..js (module.js:653:10) at Module.load (module.js:561:32) at tryModuleLoad (module.js:504:12) at Function.Module._load (module.js:496:3) at Module.require (module.js:586:17) at require (internal/module.js:11:18)

I tried to install sqlite3 with node and everything works.

Steps to reproduce

  • Clone test repo https://github.com/endenwer/electron-quick-start
  • Run command to setup project: npm i && npm run rebuild-sqlite
  • Run normal electron app: npm start.
  • Open console and ensure no errors are present: View > Toggle Developer Tools
  • Run electron app with compiled cljs: npm run compile-cljs && npm run start-cljs

Test notes

  • Cljs code is here /src/app.cljs. It only requires sqlite3 and do nothing
  • Error in electron console is different if run shadow-cljs release app
  • If change target to node-script and add main function to cljs it will compile it without errors.

I spend two days trying to solve this problem but I am out of ideas. I don't know if it is even possible with current version of shadow-cljs.

Most helpful comment

You can't compile sqlite3 for the browser since its not compatible with the browser.

You can however just set :js-options {:js-provider :require} in your build config.

{:source-paths ["src"]
 :builds {:app {:target :browser
                :output-dir "cljs_output"
                :js-options {:js-provider :require}
                :modules {:main {:entries [app]}}}}}

Typically a Browser doesn't have a require function but electron does so that should work. This way shadow-cljs won't process your node_modules at all and instead lets electron load them.

All 3 comments

You can't compile sqlite3 for the browser since its not compatible with the browser.

You can however just set :js-options {:js-provider :require} in your build config.

{:source-paths ["src"]
 :builds {:app {:target :browser
                :output-dir "cljs_output"
                :js-options {:js-provider :require}
                :modules {:main {:entries [app]}}}}}

Typically a Browser doesn't have a require function but electron does so that should work. This way shadow-cljs won't process your node_modules at all and instead lets electron load them.

Thanks. It fixed error on compilation, but it is not working yet if I run compile or watch. If I run release everything works.

Fixed it. Thanks a lot for help. I am closing this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jiyinyiyong picture jiyinyiyong  路  6Comments

lilactown picture lilactown  路  6Comments

arichiardi picture arichiardi  路  11Comments

jiyinyiyong picture jiyinyiyong  路  8Comments

tekacs picture tekacs  路  10Comments