2

My application depends on library which uses ecma6 syntax for modules:

require lib = 'lib.js' // myapp.js
import Foo from 'bar' // lib.js
...

I want to run node myapp.js, but it results in syntax error, since node does not support ecma6 modules (and --experimental-modules would require lib.js to be named lib.mjs).

Is there any way to run myapp.js without changing the source code of lib.js?

Ford O.
  • 1,394
  • 8
  • 25
  • What lib is it? Virtually all libs around are distributed in bundled or transpiled form, so they don't need ES modules to be supported. It would be a pain to use such lib, not just in Node but anywhere else. – Estus Flask Apr 20 '19 at 08:14
  • @estus https://github.com/wdanilo/basegl – Ford O. Apr 25 '19 at 08:02
  • 1
    I see. It's unnatural for a lib to expose only ES modules. I suppose this one was published as such because it's primarily used by its maintainers and suits their needs. Consider opening an issue. As of now, it needs to be processed with Babel or Webpack/Rollup. – Estus Flask Apr 25 '19 at 08:12
  • You can also modify Node loader to use js instead of mjs for specific ES modules, see https://stackoverflow.com/questions/50846189/how-to-trick-node-js-to-load-js-files-as-es6-modules – Estus Flask Apr 25 '19 at 08:13

1 Answers1

2

look at https://babeljs.io/, you can compile your code (with that lib) to usual format

Егор Лебедев
  • 1,161
  • 1
  • 10
  • 26