Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot include js file in Electron

In my HTML file, I included a script like:

<script src="js/index.js"></script>

In my script, I try to add a configuration.js file just by writing const Configuration = require("./configuration");. configuration.js is in the same folder with index.js. But on console it says:

Uncaught Error: Cannot find module './configuration'

configuration.js and index.js files are both in /app/js/ folder.

What is a solution to that? I can include Node.js modules like Lodash for example.

like image 744
nope Avatar asked Mar 08 '16 14:03

nope


1 Answers

If you want to understand what is happening when you require a module, you could read the docs.

On the first look, your code snippets should work. To require a module inside node.js, you always use the path from the file.

But, in your case, you are just importing plain JS. In this case, the script runs out of your HTML call.

That logic could cause a lot of other problems, so I would recommend you to create your own modules. node.js makes that very easy.

var configuration = {a: 1,b: 2};
module.exports = configuration;

More reading aout that:

  • https://nodejs.org/dist/latest-v5.x/docs/api/modules.html
  • http://www.sitepoint.com/understanding-module-exports-exports-node-js/

Inside your HTML file, you could put your modules together via require statements.

like image 188
apxp Avatar answered Oct 23 '22 04:10

apxp