簡體   English   中英

不能在Electron中的單獨js文件中使用require

[英]Cannot use require in a separate js file in Electron

我有一個非常簡單的Electron應用程序,使用5.0.1版本。

這是我的index.html文件:

<!DOCTYPE html>
<html>
  <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <title>Webgl</title>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/104/three.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
      <script src="./initialization.js"></script>
      <link rel="stylesheet" type="text/css" href="application.css">
  </head>

  <body>

  <script>

    initialization();

  </script>
</html>

然后我的main.js文件:

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

let win

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({ width: 1280, 
                            height: 720, 
                            nodeIntegration: true, 
                            resizable: false,
                            maximizable: false })

  // and load the index.html of the app.
  win.loadFile('index.html')

  // Open the DevTools.
  win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })

}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (win === null) {
    createWindow()
  }
})

我的package.json文件:

{
  "name": "estimation",
  "version": "1.0.0",
  "description": "estimation app",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron": "^5.0.1"
  }
}

然后我的inittialization.js文件包含initialization()方法和const fs = require('fs') ;

const fs = require('fs');

function initialization(){
}

現在我在多個地方問了同樣的問題,我嘗試了多種解決方案,沒有任何效果。 我想知道這個階段是否是電子漏洞。

錯誤我無法擺脫,我不斷得到我做的是這個Uncaught ReferenceError: require is not defined

我只想在另一個JS文件中使用require。 為什么node.js或者電子沒有把它拿起來?

將nodeIntegration放在webPreferences屬性中

{ width: 1280,
 height: 720,
 webPreferences : { nodeIntegration: true }, 
 resizable: false,
 maximizable: false
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM