while 运行 JS app uncaught exception error require is not defined 发生
while running JS app uncaught exception error require is not defined occurs
我通过视频 link 使用 Electron 创建了一个简单的 java 基于脚本的应用程序。 https://www.youtube.com/watch?v=TnXz_nnQZrwI
在尝试创建应用程序时出现以下错误,即使我将 nodeIntegration 启用为 true
index.html:12 未捕获的 ReferenceError: require 未定义
在 index.html:12
不知道怎么解决,求助
这是我的 js 和 Html 文件
我的index.js文件
const { app, BrowserWindow } = require('electron');
const os = require('os-utils');
const path = require('path');
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences:{
nodeIntegration: true
}
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// Open the DevTools.
mainWindow.webContents.openDevTools();
os.cpuUsage(function(v){
console.log('CPU Usage (%): '+ v*100);
mainWindow.webContents.send('CPU',v*100);
console.log('Mem Usage (%): '+ os.freememPercentage()*100);
mainWindow.webContents.send('Mem',os.freememPercentage()*100);
console.log('Total Mem (GB): '+ os.totalmem()/1024);
mainWindow.webContents.send('Total-Mem',os.totalmem()/1024);
});
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
我的index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<h1> Hello World!</h1>
<p>Welcome to your Electron application.</p>
<script>
const electron = require('electron');
const ipcRenderer = electron.ipcRenderer;
ipcRenderer.on('CPU',(event,data)=>{
console.log('CPU %' + data);
});
ipcRenderer.on('Mem',(event,data)=>{
console.log('Mem Usage %' + data);
});
ipcRenderer.on('Total-Mem',(event,data)=>{
console.log('Total Mem (GB)' + data);
});
</script>
</body>
</html>
对于 Electron >= 12,如果没有 contextIsolation: false
,nodeIntegration: true
将无法工作。
https://www.electronjs.org/docs/breaking-changes#default-changed-contextisolation-defaults-to-true
或者,如果您不想通过设置 nodeIntegration: true
、 来承担任何安全风险,您可以通过在“preload-file” 在创建渲染器 window 之前调用。
在 main.js 中创建主渲染器 window,如下所示:
function createWindow () {
win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: false
}
})
win.loadFile('index.html')
}
重要的部分是preload: path.join(__dirname, 'preload.js')
。
在您的 preload.js
中,您创建了一个全局变量 ipcRenderer
,如下所示:
process.once('loaded', () => {
global.ipcRenderer = electron.ipcRenderer;
});
然后 ipcRenderer
可以在您的渲染器进程中使用' html 代码就像这样:
<script>
...
ipcRenderer.send(...);
...
</script>
我通过视频 link 使用 Electron 创建了一个简单的 java 基于脚本的应用程序。 https://www.youtube.com/watch?v=TnXz_nnQZrwI
在尝试创建应用程序时出现以下错误,即使我将 nodeIntegration 启用为 true
index.html:12 未捕获的 ReferenceError: require 未定义 在 index.html:12
不知道怎么解决,求助 这是我的 js 和 Html 文件
我的index.js文件
const { app, BrowserWindow } = require('electron');
const os = require('os-utils');
const path = require('path');
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences:{
nodeIntegration: true
}
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// Open the DevTools.
mainWindow.webContents.openDevTools();
os.cpuUsage(function(v){
console.log('CPU Usage (%): '+ v*100);
mainWindow.webContents.send('CPU',v*100);
console.log('Mem Usage (%): '+ os.freememPercentage()*100);
mainWindow.webContents.send('Mem',os.freememPercentage()*100);
console.log('Total Mem (GB): '+ os.totalmem()/1024);
mainWindow.webContents.send('Total-Mem',os.totalmem()/1024);
});
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
我的index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<h1> Hello World!</h1>
<p>Welcome to your Electron application.</p>
<script>
const electron = require('electron');
const ipcRenderer = electron.ipcRenderer;
ipcRenderer.on('CPU',(event,data)=>{
console.log('CPU %' + data);
});
ipcRenderer.on('Mem',(event,data)=>{
console.log('Mem Usage %' + data);
});
ipcRenderer.on('Total-Mem',(event,data)=>{
console.log('Total Mem (GB)' + data);
});
</script>
</body>
</html>
对于 Electron >= 12,如果没有 contextIsolation: false
,nodeIntegration: true
将无法工作。
https://www.electronjs.org/docs/breaking-changes#default-changed-contextisolation-defaults-to-true
或者,如果您不想通过设置 nodeIntegration: true
、 来承担任何安全风险,您可以通过在“preload-file” 在创建渲染器 window 之前调用。
在 main.js 中创建主渲染器 window,如下所示:
function createWindow () {
win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: false
}
})
win.loadFile('index.html')
}
重要的部分是preload: path.join(__dirname, 'preload.js')
。
在您的 preload.js
中,您创建了一个全局变量 ipcRenderer
,如下所示:
process.once('loaded', () => {
global.ipcRenderer = electron.ipcRenderer;
});
然后 ipcRenderer
可以在您的渲染器进程中使用' html 代码就像这样:
<script>
...
ipcRenderer.send(...);
...
</script>