requireが定義されておらず、ロードモジュールにElectronレンダリングプロセスが与えられている



Require Is Not Defined



// main process const {app, BrowserWindow, ipcMain} = require('electron') app.on('ready', () => { // New Window const win = new BrowserWindow() // open development tools win.webContents.openDevTools() // load the page window win.loadFile('./layout/index.html') }) // render process const ele = require('electron') console.log(ele)

電子インターフェースコンソールが出力されますが、コンソールエラー、Uncaught ReferenceError: require is not defined

解決策1:



名前が変更されたかどうかを確認するにはrequireプロジェクトを使用する場合jQuery/RequireJS/Meteor/AngularJSまた、他のフレームワークを使用する場合は、ページがフレームの上に読み込まれる前に、まずノードまたは特性を無効にする必要があります。require名前の変更と削除delete window.requiredelete window.exportsdelete window.module
具体的な解決策は次のとおりです。

// In the main process. const { BrowserWindow } = require('electron') let win = new BrowserWindow({ webPreferences: { nodeIntegration: false } }) win.show()

または



window.nodeRequire = require delete window.require delete window.exports delete window.module <-! Reintroduction jq and other frameworks necessary js <=== I put it as a comment ->

使用後requireローカルを使用する必要がありますnodeRequire代わりに。

私はとても慣れていました

// render process window.nodeRequire = require delete window.require delete window.exports delete window.module const ele = nodeRequire('electron') console.log(ele)

解決策2:



主なプロセスコードは次のとおりです。

const {app, BrowserWindow, ipcMain} = require('electron') const path = require('path') app.on('ready', function() { // Create page const win = new BrowserWindow({ webPreferences: { nodeIntegration: true } }) // open the Developer Tools win.webContents.openDevTools() // Render the import process win.loadFile('./layout/index.html') })

レンダリングプロセスコードは次のとおりです。

const ele = require('electron') console.log(ele)

元のコードベースでは、文を増やし、完全なノードtrueをサポートするように変更しました。

webPreferences: { nodeIntegration: true }

正しく実行するためのコード、エラーは必要ありませんが、問題は、フレームワークnode.jsとの競合を回避するために、調査前に多くの情報が言及されていたことです。ドキュメントが言及されているnodeIntegrationのデフォルトはtrueです。

解決策3:

1.ファイルrenderer.jsを作成します

global.electron = require('electron')


2、ファイルmain.jsを変更します
プリロード構成アイテムを追加して、作成入口コードブラウザーを変更します。ファイルのプリロードとしてのrenderer.js。

win = new BrowserWindow({ width: 1000, height: 800, webPreferences: { javascript: true, plugins: true, nodeIntegration: true, // whether integrated Nodejs webSecurity: false, preload: path.join (__ dirname, '../public/renderer.js') // but within pre-loaded js files can still use the API Nodejs } })

3、電子アセンブリで使用されるようにReact

const electron = window.electron

通信プロセスを使用しているため、レンダリングプロセスで直接書き込むことができます。

const ipcRenderer = window.electron.ipcRenderer

現時点では、TypeErrorはありません。fs.existsSyncはエラーの関数ではありません。

もちろん、これらの問題を抱える主な生の産物は、フロントエンドエンジニアリングがノード環境で使用されていないために反応することです。