简体   繁体   中英

ElectronJS, JavaScript and Python - require is not defined tried all the options

I have a problem when I try to execute the following line:

var path = require('path') 

i get

Uncaught ReferenceError: require is not defined

all work fine except from require error, why?

i have 3 files:

translate.js:

function get_translate(){
var path = require('path') 
var trans = document.getElementById("trans").value
document.getElementById("trans").value = ""

var options = {
scriptPath : path.join(__dirname, '/../engine/'),
args : [trans]
}
translate = PythonShell.run('translate_engine.py', options);
translate.on('message', function(message) {
swal(message);
})
}

translate.html:

<head>
<link rel="stylesheet" 
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
<script src="linkers/translate.js"></script>
</head>

<body>
<br>
<div class="container">
<button="btn btn-info"><a style="color:white" href="gui.html">Back</a> 
</button>

<div class="jumbotron">
  <h1>Translate App</h1>
  <br>
  <label>Enter you word here</label>
  <input id="trans" type="text" placeholder="Text"/>
  <button class="but but-success" onclick="get_translate()">Go</button>
</div>
</body>

translate_engine.py:

import sys
trans = sys.argv[1]
print(trans)
sys.stdout.flush()

thank's

Are you trying to execute this code in an electron BrowserWindow?

If yes, check if nodeIntegration isn't set to false when initiating the BrowserWindow .

mainWindow = new BrowserWindow({
  minWidth: 370,
  minHeight: 520,
  webPreferences: {
    nodeIntegration: true,
  },
});

nodeIntegration is actually true by default, so you can remove this line as well – I'm just trying to make it super obvious ;)

As long as nodeIntegration is true you can run any nodeJS code in the electron render process.

This goes beyond your initial question and it's a little bit more advanced, but If you don't want to enable this as it comes with numerous security issues , you can just expose certain nodeJS methods to the render process. You can do this with a preload script.

mainWindow = new BrowserWindow({
  ...
  webPreferences: {
    nodeIntegration: false,
    preload: path.join(__dirname, 'preload.js') // here you can re-expose certain methods such as `require`
  },
});

preload String (optional) - Specifies a script that will be loaded before other scripts run in the page. This script will always have access to node APIs no matter whether node integration is turned on or off. The value should be the absolute file path to the script. When node integration is turned off, the preload script can reintroduce Node global symbols back to the global scope.

More infos can be found here: https://electronjs.org/docs/api/process#event-loaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM