d3 / d3

Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:

Home Page:https://d3js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to require D3 modules in Node

ruthwtalbot opened this issue · comments

Filed this here because it affects all the d3-* packages we’ve tried. Let me know if there is a better place to file or if you need more information!

We need the ability to require() D3 modules in Node using regular Node require() calls and the module.createRequire() function. Currently, Node isn’t able to recognize the UMD export in the D3 module package.json when it’s required, leading to the error below. Requiring the modules works fine with Browserify, but doesn’t work with Node directly.

ERROR: Error in undefined: Package subpath './dist/d3-scale.min' is not defined by "exports" in …./node_modules/d3-scale/package.json

We cannot import the modules because we want to be able to pipe the require() function to files that live outside of the current server’s directory that have their own versions of the D3 modules, so the paths need to resolve relative to those external folders, not the server folder.

We tried adding in "require" to the conditional exports in package.json for the D3 module, but it still gives the same error.

"exports": { "require": "./dist/d3-scale.min.js", "umd": "./dist/d3-scale.min.js", "default": "./src/index.js" },

Thank you for looking, really appreciate it!

I routinely require D3 modules in Node without any issues.

Could you please provide an example project setup that reproduces the error?

I work with Ruth, and tested this just now:

  1. Created a new folder with a fresh package.json.
  2. Installed d3-scale, which gave me version "4.0.0"
  3. Started Node
  4. require("d3-scale") - throws ERR_REQUIRE_ESM
  5. require("d3-scale/dist/d3-scale.min.js") - throws ERR_PACKAGE_PATH_NOT_EXPORTED

This isn't just a d3-scale issue, it breaks for other D3 packages as well, but that's the one we expect to work in Node because it lacks DOM dependencies.

D3 modules now require ES modules. If you don’t want to use ES modules, you’ll need to stick with the previous major version.

This version worked for me: npm install --save d3@6.7.0