Issue in requiring a custom module

2019-08-26 09:26发布

Let's give the context: I am currently working on making a little library for Electron.

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:

Uncaught Error: Cannot find module './my-module-two'

Irritating.

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

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! :)

1条回答
男人必须洒脱
2楼-- · 2019-08-26 10:19

Maybe you should try use require('./js/my-module/my-module-one') instead of <script src="js/my-module/my-module-one.js"></script>

查看更多
登录 后发表回答