Невозможно использовать require в отдельном js-файле в Electron - PullRequest
0 голосов
/ 21 мая 2019

У меня очень простое приложение 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

Я просто хочу использовать require в другом файле JS. Почему node.js или электрон не улавливают это?

1 Ответ

0 голосов
/ 22 мая 2019

Поместите nodeIntegration внутри атрибута webPreferences

{ width: 1280,
 height: 720,
 webPreferences : { nodeIntegration: true }, 
 resizable: false,
 maximizable: false
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...