需要自定义模块的问题 [英] Issue in requiring a custom module
问题描述
让我们给出背景:我目前正在为电子创建一个小型库.
Let's give the context: I am currently working on making a little library for Electron.
从一开始,我就在处理包含类的单个JS文件.
但是我决定改用其他类(以及另一个JS文件)来拥有更好的组织.
Since the beginning, I was working on a single JS file containing a class.
But I decided to make a different class (and so another JS file) to have a better organization.
所以我做到了.我制作了此文件,并像在许多网站上阅读的一样在文件的末尾导出了文件.
So I made it. I made this file, with an export at the end like I read on many websites.
不幸的是,当我尝试在第一堂课上要求第二堂课时,出现此错误:
Unfortunatly, when I try to require my second class in my first one, I get this error:
未捕获的错误:找不到模块'./my-module-two'
Uncaught Error: Cannot find module './my-module-two'
令人讨厌.
我在Google上搜索了大约一个小时,以找到解决方案,但我尝试了很多却没有结果.
I searched for about one hour on Google in order to find a solution, I tried many without result.
现在有我的文件内容.我断定自己认为没有用.
There my file contents right now. I cut that I judged unuseful.
my-module-one.js
// I also tried to require here.
class MyModuleOne
{
constructor() {
this.MyModuleTwo = require('./my-module-two');
}
}
my-module-two.js
class MyModuleTwo
{
// Literally nothing. But I tried with a constructor for the same issue.
}
module.exports.MyModuleTwo = MyModuleTwo;
index.html
<!-- Into <head> tags. -->
<script src="js/my-module/my-module-one.js"></script>
我的文件在同一目录中
My files are in the same directory:
myApp/
web/
js/
my-module/
my-module-one.js
my-module-two.js
node_modules/
(other directories)/
main.js
package.json
package-lock.json
当然,在 index.html 或 my-module-one.js 上需要npm安装的所有库都可以用作地狱.
Of course, all libraries installed with npm required on index.html or my-module-one.js works as hell.
感谢那位将成为我救星的人! :)
Thanks to the one who will be my savior! :)
推荐答案
也许您应该尝试使用require('./js/my-module/my-module-one')
而不是<script src="js/my-module/my-module-one.js"></script>
Maybe you should try use require('./js/my-module/my-module-one')
instead of <script src="js/my-module/my-module-one.js"></script>
这篇关于需要自定义模块的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!