javascript - Cannot include js file in Electron - Stack Overflow

In my HTML file, I included a script like:<script src="jsindex.js"><script>In m

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.

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.

Share Improve this question asked Mar 8, 2016 at 14:36 nopenope 7713 gold badges13 silver badges30 bronze badges 2
  • 3 change require("./js/configuration"); maybe done. – Haydar C. Commented Mar 8, 2016 at 14:45
  • @AhbapAldirmaz it worked, actually. I don't understand why though. If you can add an answer, I'll mark it correct. edit: Oh, I guess the path is relative to the html file. That solves it. – nope Commented Mar 8, 2016 at 14:48
Add a ment  | 

1 Answer 1

Reset to default 6

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 remend 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/dist/latest-v5.x/docs/api/modules.html
  • http://www.sitepoint./understanding-module-exports-exports-node-js/

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743753675a4501363.html

相关推荐

  • javascript - Cannot include js file in Electron - Stack Overflow

    In my HTML file, I included a script like:<script src="jsindex.js"><script>In m

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信