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.

nerdoc
  • 1,044
  • 10
  • 28
  • 1
    Seems like the environment you are executing the code in does not support ES6 modules. – Felix Kling Sep 12 '19 at 12:16
  • 1
    @FelixKling or the files are not being imported as modules. If in the browser, the ` – Pointy Sep 12 '19 at 12:25
  • 1
    @nerdoc note that the error you're getting is about the `import` keyword, not your identifier. – Pointy Sep 12 '19 at 12:26
  • 1
    If this is about NodeJS, then [this article may help](https://blog.logrocket.com/es-modules-in-node-js-12-from-experimental-to-release/) – Pointy Sep 12 '19 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 Sep 13 '19 at 14:18

0 Answers0