Skip to content

Unable to import symbols exported from imported modules #18001

@dandv

Description

@dandv

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.invalidIssues and PRs that are invalid.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions