“未定义要求"当我尝试将 js 文件(带有节点模

quot;require is not definedquot; error comes when i try to import js file (with node modules) to my main electron js file(“未定义要求当我尝试将 js 文件(带有节点模块)导入到我的主电子 js 文件时出现错误)
本文介绍了“未定义要求"当我尝试将 js 文件(带有节点模块)导入到我的主电子 js 文件时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my folder folder structure

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

Hi im new to electron.js I was facing for an issue where that i cannot capture jquery events in my main.js file. As a solution i created a separate file (events.js)[now i can capture jquery events] and i connect it to 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. Without any import library , it worked.

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

推荐答案

win = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true
    }
  })

win.loadURL 中删除 webPreferences 并添加到 BrowserWindow 选项.下面是在 BrowserWindow

Remove webPreferences from win.loadURL and add to BrowserWindow option. The below is the sample code to enable the Node api in BrowserWindow

// 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')

这篇关于“未定义要求"当我尝试将 js 文件(带有节点模块)导入到我的主电子 js 文件时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)