fur866
fur866

Reputation: 413

How to import a JS file in another JS file in ES6?

I have got this node server and a bunch of JS classes in my js directory. I want to create a file called "exports.js" that exports all the classes required by server (using exports.Classname = class notation). However, the problem is that exports.js doesn't have access to the classes. I was wondering what's the correct syntax for importing the whole ES6 class in another file. So far I have tried following with no luck:

//I want to import User class from User.js
import "./User.js"; 
import "User";
import "./User";

Any help would be much appreciated.

Note: Not that it makes any difference but please note that I am using Babel transpiler.

Upvotes: 9

Views: 23241

Answers (1)

Alessia
Alessia

Reputation: 959

// user.js
class user{
    ...
}
export default user

// another js
import user from './user.js'

Upvotes: 18

Related Questions