1

This answer: How can I use ES6 in webpack.config.js?
seems to imply a transpilation step.

Can ES6 be used natively? How?

For example I tried to convert the simple example here from require to import and receive the following error

(function (exports, require, module, __filename, __dirname) { import path from 'path'
                                                                     ^^^^
SyntaxError: Unexpected identifier
martixy
  • 784
  • 1
  • 10
  • 26
  • @KirkLarkin Oh. Seems I accidentally stumbled on the one feature that's still experimental. How could you pass this flag to node when building your application? E.g. the simple example is built using `npx webpack`, but `npx webpack --experimental-modules` does not work. – martixy Sep 03 '18 at 10:23
  • 1
    https://github.com/webpack/webpack/issues/6072 – Kirk Larkin Sep 03 '18 at 11:10
  • @KirkLarkin Hm... that's rather obscure([docs](https://webpack.js.org/api/cli/#config-options) for the option). Seems to me it still involves a transpilation(or some kind of similar shim to the process, not sure how esm works). Nevertheless, it does work, and is fairly simple and explicit. Thanks. In the absence of native support, this at least seems like a good option. – martixy Sep 03 '18 at 12:32

1 Answers1

1

You should use webpack-cli --config-register (-r) to allow that.

To use that flag, you must have babel-register installed. webpack --config-register babel-register --config ....

Another option is esm.

martixy
  • 784
  • 1
  • 10
  • 26
PlayMa256
  • 6,603
  • 2
  • 34
  • 54
  • Kirk Larkin already pointed to some github issue that hinted at the option. Though I'd use esm rather than babel. In any case, this is, at best a workaround. Waiting for ESM to land properly in node I suppose. – martixy Sep 04 '18 at 22:03
  • @PlayMa256 the link is broken. Please update your answer – J'e Jun 26 '19 at 12:16