简体   繁体   中英

JavaScript in Electron app file referenced with a script tag in a HTML form unable to use a node module with require

I am building an Electron application with some HTML that has JavaScript file referenced with a script tag in an HTML form, I am unable to use a node module with require

my function name

function sayhello() {
   
    const path = require('path');
    var knex = require("knex")({
      client: "sqlite3",
      connection: {
        filename: path.join(__dir, 'db.sqlite')
      }
    });

}

How can I reference my node modules in the function?

use nodeIntegration: true into your electron.js main window container file. If you want to use node.js functionality into renderer file.

like this,

win = new BrowserWindow({
   width: 384,
   height: 696,
   webPreferences: {
     devTools: false,
     nodeIntegration: true,
     preload: path.resolve(path.join(__dirname, "preload.js"))
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM