How to properly export two classes on a single index.js file using Javascript

javascript export multiple functions
javascript export class
javascript export default
unexpected token export
javascript import class from another file
module.exports function with parameters
javascript import variable from another file
module.exports class

I have two different classes:

Class1.js

class Class1 { 
    initialize = () => {
        console.log("Class1 Initializer");
    }
}

export default Class1;

Class2.js

class Class2 { 
    initialize = () => {
        console.log("Class2 Initializer");
    }
}

export default Class2;

classes/index.js

import Class1 from "./Class1";
import Class2 from "./Class2";

export default { 
    Class1,
    Class2
}

Then I'm trying to use the classes:

server.js

import { Class1, Class2 } from "./classes";

...
Class1.initialize() <<<<==== ERROR - Cannot read property `initialize` of undefined.

What is the correct syntax of the imports and exports? Why is that error happening?


My understanding is, your method of exporting is possible and that this problem is caused by initialize() being an instance method of Class1. You'll need to create an instance of Class1 before calling initialize(). One way to instantiate Class1 would be via new:

import { Class1, Class2 } from "./classes";

// Create "new" instance of Class1, and then call initialize() on it
(new Class1()).initialize()

Can you export multiple classes from a single Nodejs Module , And access these classes as you have correctly mentioned: var People index.​js const { Jack: personJack, John: personJohn, } = require('. Export default. In practice, there are mainly two kinds of modules. Modules that contain a library, pack of functions, like say.js above.; Modules that declare a single entity, e.g. a module user.js exports only class User.


Couple things here.

First, you're using arrow syntax in your class methods. Arrow functions don't bind to their own "this" the same way that normal functions do. I'd recommend refactoring that code to mirror below. Do the same for both Class1 and Class2.

class Class1 {
    initialize() {
        console.log("Class1 Initializer");
    }
}

Two, if you're using the ES6 export syntax, you can only ever have one default. In your index.js file, you'll want to just to export { Class1, Class2}, but you can't use export default { Class1, Class2 }.

Three, you need to create an instance of the class before you can call its method. You can either create a new variable that initializes a new instance of the class, then call the method, or you can do it all in-line.

new Class2().initialize();

Fourth, you're importing from a file called a file called "./classes". As far as I can tell that doesn't exist. It should be from "./index".

Hope this helps!

Export and Import, Export and import directives have several syntax variants. Please note that export before a class or a function does not make it a function expression. Now if we only use one of say.js functions in our project: As there may be at most one default export per file, the exported entity may have no name. The export statement is used when creating JavaScript modules to export live bindings to functions, objects, or primitive values from the module so they can be used by other programs with the import statement. Bindings that are exported can still be modified locally; when imported, although they can only be read by the importing module the value updates whenever it is updated by the exporting


You are using default on object literal. You cant do that.

Do below in classes/index.js:

import Class1 from "./Class1";
import Class2 from "./Class2";

export { 
    Class1,
    Class2
}

and then use :

import * as Classes from "./classes";

Then :

new Classes .Class2().initialize();

By the way how are running above example? its like node server.js?? Node 8.5.0 + supports ES6 like import and export style and file extension also should be .mjs otherwise .js file will need to use require function

export, The export statement is used when creating JavaScript modules to export class ClassName {. There are two different types of export, named and default. You can have multiple named exports per module but only one default export. You can't run JS modules via a file:// URL — you'll get CORS errors. Using index.js for Fun and Public Interfaces Conroy Whitney By using an index.js file in the root of each folder to re-export a subset of files, you can effectively create explicit public interfaces for your React modules.


How to use module.exports in Node.js, A module is a discrete program, contained in a single file in Node.js. system of modules, but there are other module types used in the JavaScript ecosystem. Important Question #2: How does a module share its code with other modules? For an example exporting a class that was created with ES6 syntax, see the Book​  This is how you can write code in one file and share that code so it can be used by another file or files. The examples I’m going to walk through in this post will be using Node.js and React.js.


Understanding Imports and Exports with JavaScript ES6/ES2015, Here, we export two classes, one constant, and one function. Of note, you will notice our first export is named default. With ES6 each module can have ONE  The language specification ECMAScript 2015, often referred to as ES6, introduced classes to the JavaScript language. Classes in JavaScript do not actually offer additional functionality, and are often described as providing “syntactical sugar” over prototypes and inheritance in that they offer a cleaner and more elegant syntax.


Using Classes, It does not add a new object-oriented inheritance model to JavaScript. In this In main.js , remove the import statement at the top of the file. html += ` <tr> <td>${​index + 1}</td> <td class="currency">${Math.round(year. Part 2: Using Classes in Modules export default class Mortgage { constructor(principal, years, rate)  So, you can invoke the function using parenthesis (). Run the above example and see the output as shown below. C:\> node app.js Hello World Export Function as a Class. In JavaScript, a function can be treated like a class. The following example exposes a function that can be used like a class.