9

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.

5
  • If using babel, then import is fine. If not, you have to use require(). Babel transforms import in require() Commented Dec 17, 2016 at 0:35
  • Of the three given, what's the correct syntax for import? Commented Dec 17, 2016 at 0:38
  • 3
    Did you read the MDN documentation? developer.mozilla.org/en/docs/Web/JavaScript/Reference/… . It lists all the possible ways how to import a module. Commented Dec 17, 2016 at 0:43
  • Use the last one. But first you need to do export default [YOUR CLASS] in the file your define User. Commented Dec 17, 2016 at 5:03
  • 1
    Also, if you want to use / reference the class after import you should use import User from './User', then you can reference it Commented Dec 19, 2016 at 8:14

1 Answer 1

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

// another js
import user from './user.js'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.