Module.export does not export the file to be imported into main.js

I am new to Javascript and I use Visual Code as the IDE for working with Javascript.

I am learning some lessons, and I ran into a problem that I cannot solve it.

I use module.export to export my js file and I use it to use it in the main file. However, it does not work, as I expect a warning message to appear.

Below is my code

    **task.js**
    /* jshint node: true */
    'use strict';
    class Task {

            constructor(name) {
                this.name = name;
            }

            completed() {
                alert(this.name);
            }
     }
     module.export = Task;

     **cat.js**
    /* jshint node: true */
    'use strict';
    function Cat(name) {
        this.name = name;

        this.toString = function() {
                alert(name);
        };
    }

    Cat.prototype.complete = function() {
        console.log('Completed');
    };
    module.export = Cat;

    **main.js**
    var Task = require('./task');
    var Cat = require('./cat');
    // Ways to create instances.

    var task1 = new Task("A");

    task1.completed();


    var cat1 = new Cat("Fluffy");
    cat1.toString();
    cat1.complete();

  **Demo1.html**
    <!DOCTYPE html>
    <html>
        <head>

        </head>
        <body>
        <h1> Hello! </h1>
        <p id="demo"></p>

        <script type="text/javascript" src="script/main.js"></script>
        </body>
    </html> 

Directory structure:

.vscode
   script
     cat.js
     task.js
     main.js
demo1.html

I do not get errors on the output console. Any help would be greatly appreciated.

+4
source share
1 answer

It looks like you just have a typo! Use module.exportsinstead module.export.

Node.js Documentation

javascript, export (Export Doc), . , .

+1

Source: https://habr.com/ru/post/1684030/


All Articles