I'm new to Electron and Node. I can't seem to require()
a local .js file from another one in Electron and I don't understand the problem. I think what I'm missing is something pretty simple, but I can't find it.
Here's my file structure:
/
package-lock.json
node_modules
main.js
package.json
game/
...
test.js
properties.js
assets/html/
main_window.html
main.js
only loadsmain_window.html
into an Electron window and not much else.- I reference to
test.js
from inside the html file using<script src="../../game/test.js"></script>
. Here's
test.js
:const properties = require('./properties'); ...
In return I get this error in Dev Tools console:
Uncaught Error: Cannot find module './properties'
When I use node test.js
command in the game
folder, the file gets imported, everything works just fine. But when I switch to Electron and use npm start
from root I think I'm requiring the local .js file as it should be, by putting a dot and a forward slash before its name. And despite being in the same folder as the test.js
file, it can't seem to locate properties.js
.
Also package.json
file, just in case it might be needed:
{
"name": "economy-board-game-electron",
"version": "1.0.0",
"description": "Recreation of that board game for educational purposes.",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"author": "Edvin Boul",
"license": "MIT",
"dependencies": {
"electron": "^4.0.1"
}
}
properties
relative to the HTML file worked (require('./../../game/properties')
). So I put the HTML file in the same folder as the .js files to not backtrack that much. Thank you very much. You can post it as an answer if you'd like so I can accept it.