Import as ECMAScript module
mna opened this issue · comments
Hello,
After reading the docs, I decided to try out mojiscript, but after installing the library as a dependency in my project (npm i mojiscript --save
) I tried to import it in my src/main.mjs
file using the ES module syntax, as shown in the docs, but it didn't work for me:
import { map } from 'mojiscript'
$ node --experimental-modules src/main.mjs
(node:6834) ExperimentalWarning: The ESM module loader is experimental.
file:///.../src/main.mjs:1
import { map } from 'mojiscript'
^^^
SyntaxError: The requested module 'mojiscript' does not provide an export named 'map'
If instead I use import map from 'mojiscript/list/map'
, I get:
(node:6849) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/esm/default_resolve.js:62
let url = moduleWrapResolve(specifier, parentURL);
^
Error: Cannot find module '/.../node_modules/mojiscript/list/map' imported from /.../src/main.mjs
I'm no expert on nodejs support of ES modules, but it seems like because mojiscript
is a CommonJS library, only the "default export" syntax is supported, so using the syntax shown in mojiscript's docs would not work?
Currently only the “default export” is supported for CommonJS files or packages
https://nodejs.org/api/esm.html#esm_interoperability_with_commonjs
Asking in case I'm missing something, I'd definitely like to use the imports as in the docs.
Thanks,
Martin
Node 12 changed the way this works. I need to make some modifications for this to work in Node 12 :|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I looked into this and it's a little bit more complicated than I thought it would be. I'll have to research some more later.
For now the workaround looks like this:
import mojiscript from 'mojiscript'
const { cond, pipe, map, range, allPass } = mojiscript
Further reading on ECMAScript module support in Node.js 12: https://2ality.com/2019/04/nodejs-esm-impl.html
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.