Skip to content

Instantly share code, notes, and snippets.

@AimForNaN
Created November 27, 2017 23:00
Show Gist options
  • Save AimForNaN/03fad282ab34ab2a1f049a4b91e6bc0d to your computer and use it in GitHub Desktop.
Save AimForNaN/03fad282ab34ab2a1f049a4b91e6bc0d to your computer and use it in GitHub Desktop.
Expose Node.js modules to web pages in Electron!
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