0

I want to get access to ES6 class from another file in my gulpfile.js. It looks like that unable to use import:

import MyClass from './development/myFramework/MyClass';

const instanceOfMyClass = new MyClass();

C:\MyIDE\projects\myProject\gulpfile.js:3 import MyClass from './development/myFramework/MyClass'; ^^^^^^ SyntaxError: Unexpected token import

Up to this point I always got the access to ES6-class from another folder by import and don't know, how to do it by require properly.

First: I know that some web-browsers don't understand ES6 modules, but what about Node.JS runtime?

And then, how I need to get access to MyClass and the properly create the instance? Is require the only way?

Takeshi Tokugawa YD
  • 670
  • 5
  • 40
  • 124
  • 1
    Use `require('./development/myFramework/MyClass');` for now. – Mika Sundland Nov 09 '17 at 00:28
  • @MikaS, thank you for the comment answer. Is the following code correct? `const MyClass = require('./development/myFramework/MyClass'); let myClassInstance = MyClass();` I suppose the shorthand `let myClassInstance = require('./development/myFramework/MyClass');` is invalid, is not it? – Takeshi Tokugawa YD Nov 09 '17 at 00:38
  • It depends on how the file with MyClass is set up. You also have to change from `export` to `module.exports` inside it. You can read more about it [here](https://medium.freecodecamp.org/node-js-module-exports-vs-exports-ec7e254d63ac). It should be quite similar to what you are used to with import/export. And I don't think you can use classes in the current version of Node without using Babel. – Mika Sundland Nov 09 '17 at 01:00

0 Answers0