0

I can't see why such a simple thing like exporting/importing an object/function in Js does not work as intended. Where is my error?

core.js:

import getPlugins from './plugins.js'

plugins.js:

export default () => {
  return ["blah/index.js"]
}

I always get the error:

import getPlugins from './plugins.js'
       ^^^^^^^^^^

SyntaxError: Unexpected identifier

WTF? AFAIK I am declaring the identifier here.

So I thought that exporting the default object (as function) is easy like this.

I tried everything. module.exports =, exporting named function:

module.exports = {
  getPlugins: () => {
    // ...
  }
}

Please help a JS beginner. Found no answer on all the tutorials/explains I saw - or - I didn't get it at least.

5
  • 1
    Seems like the environment you are executing the code in does not support ES6 modules. Commented Sep 12, 2019 at 12:16
  • 1
    @FelixKling or the files are not being imported as modules. If in the browser, the <script> type must be "module". In Node, things are slightly (but not much) more complicated.
    – Pointy
    Commented Sep 12, 2019 at 12:25
  • 1
    @nerdoc note that the error you're getting is about the import keyword, not your identifier.
    – Pointy
    Commented Sep 12, 2019 at 12:26
  • 1
    If this is about NodeJS, then this article may help
    – Pointy
    Commented Sep 12, 2019 at 12:29
  • Thanks all of you. Yes, I stumbled upon the module mess in Js, which hopefully will be better with ES6 - great article @Pointy. The environment is webpack, which uses node internally? I changed the import to require() and it worked. AgainWhatLearned™. Oh, and use that as answer - eh - both were correct and helpful. ;-)
    – nerdoc
    Commented Sep 13, 2019 at 14:18

0

Browse other questions tagged or ask your own question.