【问题标题】:"require is not defined" error comes when i try to import js file (with node modules) to my main electron js file当我尝试将 js 文件(带有节点模块)导入到我的主电子 js 文件时出现“未定义要求”错误
【发布时间】:2020-05-28 17:50:07
【问题描述】:

这是我的文件夹文件夹结构

您好,我是 electron.js 的新手,我遇到了一个问题,即我无法在 ma​​in.js 文件中捕获 jquery 事件。作为解决方案,我创建了一个单独的文件 (events.js)[现在我可以捕获 jquery 事件] 并将其连接到 index.html。所以在我的 event.js 我添加了一个 cron-job(node-cron) 来检查它是否工作,但是当我尝试运行一个项目时,我得到一个错误提示 require is not defined。没有任何导入库,它就可以工作。

这是我的 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>

这是我的 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();

});

这是我的 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();
})();

【问题讨论】:

    标签: javascript jquery node.js electron


    【解决方案1】:
    win = new BrowserWindow({
        webPreferences: {
          nodeIntegration: true
        }
      })
    

    从win.loadURL 中删除webPreferences 并添加到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')
    

    【讨论】:

    • 非常感谢你救了我
    猜你喜欢
    • 1970-01-01
    • 2021-06-02
    • 2016-01-19
    • 2018-07-14
    • 2020-04-14
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    相关资源
    最近更新 更多