Created
November 27, 2017 23:00
-
-
Save AimForNaN/03fad282ab34ab2a1f049a4b91e6bc0d to your computer and use it in GitHub Desktop.
Expose Node.js modules to web pages in Electron!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function escapePath (path) { | |
// Handle Windows paths! | |
return path.replace(/\\/g, '\\\\'); | |
} | |
const {app, BrowserWindow, session} = require('electron'); | |
const path = require('path'); | |
const AppPath = app.getAppPath(); | |
const NodeModules = escapePath(path.resolve(AppPath, 'node_modules')); | |
app.on('ready', () => { | |
var win = new BrowserWindow({ | |
width: 800 | |
, height: 600 | |
, autoHideMenuBar: true | |
, center: true | |
, icon: path.resolve(AppPath, 'icon.ico') | |
, title: 'Web page!' | |
, webPreferences: { | |
devTools: true | |
, nodeIntegration: true | |
} | |
}) | |
// Load paths on dom ready! | |
win.webContents.on('dom-ready', () => { | |
win.webContents.executeJavaScript(` | |
module.paths.push('${NodeModules}'); | |
`); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment