node.js - require() node module from Electron renderer process served over HTTP -


typically, in electron app, can require node modules both main process , renderer process:

var mymodule = require('my-module'); 

however, doesn't seem work if page loaded via http instead of local filesystem. in other words, if open window this:

win.loadurl(`file://${__dirname}/index.html`); 

i can require node module without problems. if instead open window this:

win.loadurl(`http://localhost:1234/index.html`); 

i no longer can require node modules inside web page - uncaught error: cannot find module 'my-module' in web page's console. there way use node modules in electron page served on http?


a little context: company building application needs ability hosted web application and inside electron shell. make simpler , consistent across both environments, electron app starts local web server , opens app hosted @ http://localhost:1234. i'd ability add spell checking/spelling suggestions application using electron-spell-check-provider. module needs imported , initialized inside renderer process, i'm trying require('electron-spell-check-provider') inside web page, fails cannot find module error.

finally figured out. in main process, figure out absolute path node_modules directory, in:

var nodemoddir = require.resolve('some-valid-module'); var dirnm      = 'node_modules'; var pos = nodemoddir.lastindexof(dirnm); if(pos != -1)     nodemoddir = nodemoddir.substr(0, pos+dirnm.length+1); 

now path renderer process via ipc. finally, in renderer can require using absolute path:

var mymod = require(nodemoddir+'some-valid-module'); 

works me electron 1.6.7.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -