[英]"require is not defined" error comes when i try to import js file (with node modules) to my main electron js file
This is my folder folder structure这是我的文件夹文件夹结构
Hi im new to electron.js I was facing for an issue where that i cannot capture jquery events in my main.js file.嗨,我是Electron.js 的新手,我遇到了一个问题,即我无法在main.js文件中捕获 jquery 事件。 As a solution i created a separate file ( events.js )[now i can capture jquery events] and i connect it to index.html .
作为解决方案,我创建了一个单独的文件( events.js )[现在我可以捕获 jquery 事件] 并将其连接到index.html 。 So in my event.js i added a cron-job(node-cron) to check whether it's working or not, but when i try to run a project i get an error saying require is not defined .
所以在我的event.js 中,我添加了一个cron-job(node-cron)来检查它是否正常工作,但是当我尝试运行一个项目时,我收到一个错误,提示require is not defined 。 Without any import library , it worked.
没有任何导入库,它工作。
This is my index.html这是我的index.html
<body>
<div style="margin-top:15px" class="container">
<div class="row">
<div class="col-xs-3">
<a class="btn btn-info btn-sm btn-block" href="./layouts/settings.html" id="menu-btn-settings"
role="button">Settings</a>
<a class="btn btn-info btn-sm btn-block" href="./layouts/health.html" id="menu-btn-health"
role="button">System Health</a>
<a class="btn btn-info btn-sm btn-block" href="./layouts/abc-now.html" id="menu-btn-abc-now"
role="button">Sync Now</a>
<a class="btn btn-info btn-sm btn-block" href="./layouts/abc-customer.html" id="menu-btn-abc-user"
role="button">Sync
Customer</a>
<a class="btn btn-info btn-sm btn-block" href="./layouts/about.html" id="menu-btn-about"
role="button">About</a>
</div>
<div class="col-xs-9">
<div id="alert-msg"></div>
<div id="content"></div>
</div>
</div>
</div>
<!-- Insert this line above script imports -->
<script>if (typeof module === 'object') { window.module = module; module = undefined; }</script>
<script src="./assets/js/jquery-3.4.1.min.js"></script>
<script src="./assets/js/bootstrap.min.js"></script>
//
<script src="./main.js"></script>
<script src="./app/events.js"></script>
<script src="./assets/js/require.js"></script>
<script>if (window.module) module = window.module;</script>
</body>
This is my main.js这是我的 main.js
app.on('ready', function () {
win = new BrowserWindow({});
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: false,
webPreferences: {
nodeIntegration: true,
},
}));
win.on('closed', function () {
app.quit();
});
win.webContents.openDevTools();
});
this is my event.js这是我的event.js
(function () {
'use strict';
var CronJob = require('cron').CronJob;
var job = new CronJob('* * * * * *', function () {
console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');
job.start();
})();
win = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
})
Remove webPreferences
from win.loadURL
and add to BrowserWindow
option.从
win.loadURL
删除webPreferences
并添加到BrowserWindow
选项。 The below is the sample code to enable the Node api in BrowserWindow
下面是在
BrowserWindow
启用 Node api 的示例代码
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.