question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

import conflict with 3.0.0 update

See original GitHub issue

Hi! I happened to pull in v3.0.0 of d3-format today and the recent type: “module” change broke an existing use case. I realize it’s a major version bump, and I hate to be a bother, but wanted to report in case it’s not an expected behavior with this change.

I have a very simple set up, entirely reproduced in this repo here. https://github.com/freeman-lab/d3-import-test

It’s a tiny next.js app that depends only on d3-import in one file using the syntax

import { format } from 'd3-format'

Previously with ^v2.0.0 this worked fine. With v3.0.0 I get this error

Error: Must use import to load ES Module: /Users/freeman/github/freeman-lab/d3-import-test/node_modules/d3-format/src/index.js
require() of ES modules is not supported.
require() of /Users/freeman/github/freeman-lab/d3-import-test/node_modules/d3-format/src/index.js from /Users/freeman/github/freeman-lab/d3-import-test/.next/server/pages/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /Users/freeman/github/freeman-lab/d3-import-test/node_modules/d3-format/src/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/freeman/github/freeman-lab/d3-import-test/node_modules/d3-format/package.json.

I’m not an expert on some of the recent module ecosystem preferences, so I’m not totally sure how to think about the problem.

I did confirm that I could get it to work by putting "type": "module" in the top-level package.json of my app. But that then made it impossible to use many other modules, including all the d3-* modules that have not yet been updated. And of course everything also worked if I rolled back to ^v2.0.0, but then I’m stuck with older versions.

So, is my understanding correct that using any of the new versions of the d3-* modules will force the "type": "module" requirement on the importing module as a whole? Or am I importing incorrectly?

Again, apologies if I’m misunderstanding, this might all be intended behavior and I just need to change the usage pattern on my side.

Thanks for all the hard work you do on these libraries!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
mbostockcommented, Apr 25, 2021

This is an issue with Next.js, not with D3, so closing. You can either stick with the previous major version, workaround it as described below, ask the Next.js team to fix it, or contribute a fix to Next.js.

I believe the workaround for Next.js involves putting something like this in your next.config.js:

function generateIncludes(modules) {
  return [
    new RegExp(`(${modules.join("|")})$`),
    new RegExp(`(${modules.join("|")})/(?!.*node_modules)`)
  ];
}

const includes = generateIncludes([
  "d3-format" // list any other ES module packages here
]);

module.exports = {
  webpack: (config, options) => {
    config.externals = config.externals.map((external) => {
      if (typeof external !== "function") return external;
      return (context, request, callback) => {
        return includes.find((i) =>
          i.test(
            request.startsWith(".") ? path.resolve(context, request) : request
          )
        )
          ? callback() // i.e., not an external
          : external(context, request, callback);
      };
    });
    return config;
  }
}; 
3reactions
cruikshanksscommented, Aug 11, 2021

Got lucky today. Updated Node version (by clicking the installer on their website) and Next.js version to latest (by yarn add next@latest). No longer have the error. Seems that Next.js team supports ESM modules now needed for import * as d3 from 'd3'. Good luck out there!

Read more comments on GitHub >

github_iconTop Results From Across the Web

conflict with Spring Boot 2.5.4 and Spring Cloud 3.0.3 - ...
I want to update to Spring Boot 2.5+ and also use Spring Cloud Consul - I am attempting to pull in: spring-cloud-starter-consul-discovery:3.0.3 ...
Read more >
How to solve dependency conflicts with Maven | by Liam
After you install or deploy the package, you can import the shaded library, there won't be conflict.
Read more >
Migrating AWS Glue jobs to AWS Glue version 3.0
You can avoid classpath conflicts in AWS Glue 3.0 with the --user-jars-first AWS Glue job parameter or by shading your dependencies. From AWS...
Read more >
Log4j – Changes - Apache Logging Services
mongo from 3.0.0 to 3.4.1. Update tests for binary incompatibilities in APIs. Thanks to Gary Gregory. ggregory. Update, Bump org.fusesource ...
Read more >
NuGet Package Dependency Resolution
The application owner can choose to upgrade Package C to a version higher than 2.0.0, thus no further downgrading the version for Package...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found