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

Unable to import symbols exported from imported modules #18001

Closed
dandv opened this issue Jan 5, 2018 · 4 comments
Closed

Unable to import symbols exported from imported modules #18001

dandv opened this issue Jan 5, 2018 · 4 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. invalid Issues and PRs that are invalid.

Comments

@dandv
Copy link
Contributor

dandv commented Jan 5, 2018

I'm trying to use d3-dsv in a script run with --experimental-modules. When importing one of the symbols that d3-dsv exports from imported modules, I get:

$ npm install d3-dsv
$ echo "import { csvFormat } from 'd3-dsv'" > exp.mjs
$ node --experimental-modules exp.mjs
(node:29364) ExperimentalWarning: The ESM module loader is experimental.
SyntaxError: The requested module does not provide an export named 'csvFormat'
    at ModuleJob._instantiate (internal/loader/ModuleJob.js:84:17)
    at <anonymous>

This works with Babel:

$ node -r babel-register exp.mjs  # OK
$ node -v
v9.3.0

A workaround is to import everything...

import d3 from 'd3-dsv';
d3.csvFormat([]);

...but that's not backwards compatible

$ node -r babel-register exp.mjs
exp.mjs:9
_d3Dsv2.default.csvFormat([]);
                ^

TypeError: Cannot read property 'csvFormat' of undefined
    at Object.<anonymous> (exp.mjs:2:4)
    at Module._compile (module.js:660:30)
    at loader (node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at startup (bootstrap_node.js:194:16)
    at bootstrap_node.js:618:3

Alternatively, importing the whole module fails and using property notation with --experimental-modules:

import * as d3 from 'd3-dsv';
d3.csvFormat([]);
$ node --experimental-modules exp.mjs
(node:31199) ExperimentalWarning: The ESM module loader is experimental.
TypeError: d3.csvFormat is not a function
    at exp.mjs:2:4
    at ModuleJob.run (internal/loader/ModuleJob.js:96:14)
    at <anonymous>
@bnoordhuis
Copy link
Member

See "main" in its package.json, that file you link to is not its entry point.

Closing, no reason to believe this is a bug in node.js.

@bnoordhuis bnoordhuis added esm Issues and PRs related to the ECMAScript Modules implementation. invalid Issues and PRs that are invalid. labels Jan 5, 2018
@bnoordhuis
Copy link
Member

Forgot to mention, if you just wanted to ask a question instead of report a bug, please use the help repo for that.

@dandv
Copy link
Contributor Author

dandv commented Jan 6, 2018

CC @mbostock - how should ES6 modules import d3-dsv?

@mbostock
Copy link

mbostock commented Jan 6, 2018

The expected pattern is

import {csvFormat} from "d3-dsv";

However, you’ll need an environment that supports the module entry in package.json (I recommend @ std/esm). (An environment that supports interoperability by importing from a CommonJS bundle would also work, but is a bit silly given that d3-dsv is authored in ES modules.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. invalid Issues and PRs that are invalid.
Projects
None yet
Development

No branches or pull requests

3 participants