Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fluture with esm #274

Closed
henriklundgren opened this issue Aug 23, 2018 · 7 comments
Closed

Fluture with esm #274

henriklundgren opened this issue Aug 23, 2018 · 7 comments

Comments

@henriklundgren
Copy link

I am unable to use Fluture with esm.

// @file index.js
import {Future} from 'fluture';
$ node -r esm index.js
SyntaxError:
@yeldiRium
Copy link

yeldiRium commented Aug 23, 2018

Have you tried import Future form 'fluture';?

I haven't used it with esm yet, but I'm using --experimental-modules and it works fine.

Edit: I am actually using it with esm (although @std/esm) for testing with mocha and it works as above.

@henriklundgren
Copy link
Author

@yeldiRium Thanks, but it seems that it's not exporting anything, named nor default.

@Avaq
Copy link
Member

Avaq commented Aug 23, 2018

Thanks for the report. I've reproduced it:

$ cd `mktemp -d`
$ npm i fluture esm
+ fluture@9.0.1
+ esm@3.0.80
added 8 packages from 3 contributors and audited 12 packages in 1.359s
found 0 vulnerabilities
$ echo 'import {reject} from "fluture";' > index.js
$ node -r esm index.js 
file:///tmp/tmp.CRUBSFiSqW/index.js:1
SyntaxError: Missing export name 'reject' in ES module: file:///tmp/tmp.CRUBSFiSqW/node_modules/fluture/index.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)

The reason is that the wrong file is being loaded (index.js, as opposed to index.mjs.js).

The package.json specifies the .mjs.js file as being the ES module:

"module": "index.mjs.js",

I'll investigate further.

@Avaq
Copy link
Member

Avaq commented Aug 23, 2018

I believe this happens because the "module" field in package.json is a "standard" set by the various build tools out there to distinguish between modular JavaScript and ESM. Node, however, decided to go with file extension to make the distinction.

So when loading a dependency with Node's --experimental-modules (or esm by extension, because it follows Node), then it will try to load index.mjs, and if it doesn't exist, fall back to index.js.

This is why index.js is being loaded, and not index.mjs.js.

This issue is loosely related to the "Upgrade to ESM" item on the Roadmap. Once I would fix the file extensions on the modular JavaScript files, Node and esm would be able to identify them, and load them appropriately.

The reason I haven't done this yet is because many tools are not ready for the change. Babel, for example, only just started acknowledging the existence of the .mjs file extension.

@Avaq
Copy link
Member

Avaq commented Aug 23, 2018

@henriklundgren I think you might be able to work around it for now by setting the "mode" option of esm to "auto".

@Avaq Avaq mentioned this issue Aug 28, 2018
@Avaq
Copy link
Member

Avaq commented Aug 28, 2018

@henriklundgren I'm working on a fix in #276 :)

@Avaq
Copy link
Member

Avaq commented Aug 29, 2018

Seems like that branch fixes the issue.

$ cd `mktemp -d`
$ npm i fluture-js/fluture#avaq/mjs esm
+ esm@3.0.80
+ fluture@9.0.2
added 8 packages from 3 contributors and audited 12 packages in 4.697s
found 0 vulnerabilities

$ echo 'import {resolve} from "fluture"; resolve(42).value(console.log);' > index.js

$ node -r esm index.js 
42

$ cp index.js index.mjs 
$ node --experimental-modules --no-warnings index.mjs 
42

I will be releasing it soon along with some additional minor breaking changes. Until then, @henriklundgren, you can install Fluture with npm install --save fluture-js/fluture#0aa28b3 to get the esm-enabled version.

@Avaq Avaq closed this as completed in #276 Aug 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants