简体   繁体   English

如何在节点和打字稿项目中导入 d3.js? [ERR_REQUIRE_ESM]

[英]How to import d3.js in a node and typescript project? [ERR_REQUIRE_ESM]

I'm working on a node project using typescript, oclif and d3.js, among other things.我正在使用 typescript、oclif 和 d3.js 等进行节点项目。 Typescript was configured by npx when creating the project with oclif so I didn't control all aspects of it (especially regarding TS, and I'm not an expert. TS is great, but configuration wise, meh). Typescript 是在使用oclif创建项目时由 npx 配置的,所以我没有控制它的所有方面(尤其是关于 TS,而且我不是专家。TS 很棒,但配置明智,嗯)。

Anyway, everything was working fine, but I recently added d3 as a dependency and tried to build a document, and I get this error when running it:无论如何,一切正常,但我最近将 d3 添加为依赖项并尝试构建文档,运行时出现此错误:

(node:22554) [ERR_REQUIRE_ESM] Error Plugin: dbacl [ERR_REQUIRE_ESM]: require() of ES Module /dbacl/node_modules/d3/src/index.js from /dbacl/src/tree/graph-tree.ts not supported.
Instead change the require of index.js in /Users/nico/Furo/Dev/dbacl/src/tree/graph-tree.ts to a dynamic import() which is available in all CommonJS modules.

In the file where I'm using d3 I import it as per documentation :在我使用 d3 的文件中,我按照文档将其导入:

import * as d3 from 'd3'

I suspect it might be a problem after compiling, so here is tsconfig.json .我怀疑编译后可能有问题,所以这里是tsconfig.json I didn't change anything since generation by npx.自 npx 生成以来,我没有改变任何东西。

{
  "compilerOptions": {
    "declaration": true,
    "importHelpers": true,
    "module": "commonjs",
    "outDir": "dist",
    "rootDir": "src",
    "strict": true,
    "target": "es2019",
  },
  "include": [
    "src/**/*"
  ]
}

And in case it's more relevant, here is my package.json file as well (didn't change anything either):如果它更相关,这也是我的package.json文件(也没有改变任何东西):

{
  "name": "dbacl",
  "version": "0.0.0",
  "description": "",
  "author": "",
  "bin": {
    "dbacl": "./bin/run"
  },
  "license": "MIT",
  "main": "dist/index.js",
  "repository": "*****/dbacl",
  "files": [
    "/bin",
    "/dist",
    "/npm-shrinkwrap.json",
    "/oclif.manifest.json"
  ],
  "dependencies": {
    "@oclif/core": "^1",
    "@oclif/plugin-help": "^5",
    "@oclif/plugin-plugins": "^2.0.1",
    "@types/d3": "^7.4.0",
    "@types/jsdom": "^16.2.14",
    "d3": "^7.6.1",
    "directory-tree": "^3.3.0",
    "jsdom": "^20.0.0",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "@oclif/test": "^2",
    "@types/chai": "^4",
    "@types/mocha": "^9.0.0",
    "@types/node": "^16.9.4",
    "chai": "^4",
    "eslint": "^7.32.0",
    "eslint-config-oclif": "^4",
    "eslint-config-oclif-typescript": "^1.0.2",
    "globby": "^11",
    "mocha": "^9",
    "oclif": "^3",
    "shx": "^0.3.3",
    "ts-node": "^10.2.1",
    "tslib": "^2.3.1",
    "typescript": "^4.4.3"
  },
  "oclif": {
    "bin": "dbacl",
    "dirname": "dbacl",
    "commands": "./dist/commands",
    "plugins": [
      "@oclif/plugin-help",
      "@oclif/plugin-plugins"
    ],
    "topicSeparator": " ",
    "topics": {
      "hello": {
        "description": "Say hello to the world and others"
      }
    }
  },
  "scripts": {
    "build": "shx rm -rf dist && tsc -b",
    "lint": "eslint . --ext .ts --config .eslintrc",
    "postpack": "shx rm -f oclif.manifest.json",
    "posttest": "yarn lint",
    "prepack": "yarn build && oclif manifest && oclif readme",
    "test": "mocha --forbid-only \"test/**/*.test.ts\"",
    "version": "oclif readme && git add README.md"
  },
  "engines": {
    "node": ">=12.0.0"
  },
  "keywords": [
    "oclif"
  ],
  "types": "dist/index.d.ts"
}

I find this problem quite confusing because everywhere I've been searching about this problem, it seems I'm doing the right thing.我发现这个问题非常令人困惑,因为我一直在寻找这个问题,似乎我在做正确的事情。 I assume the problem might be between my screen and my chair, but really I don't get it.我认为问题可能出在我的屏幕和椅子之间,但我真的不明白。 Also, should I use the dynamic import like this?另外,我应该像这样使用动态导入吗?

const d3 = await import('d3')

I tried, it didn't work.我试过了,没用。 I made the function that generates the document async and added that line at the start but nope.我制作了生成文档异步的函数,并在开始时添加了该行,但没有。 Anyway, if I believe the error, that would be the way to go but I didn't see this solution anywhere when looking for a solution to my problem.无论如何,如果我相信这个错误,那将是要走的路,但在寻找我的问题的解决方案时,我在任何地方都没有看到这个解决方案。 What do you guys think?你们有什么感想?

Thanks谢谢

So thanks to the input from a colleague and this article , the problem is that d3 now supports ESM only.所以多亏了一位同事的意见和这篇文章,问题是 d3 现在只支持 ESM。 So when the compiler turns the import into所以当编译器把导入变成

// index.ts
import * as d3 from 'd3'

// compiled index.js
const d3 = require('d3')

it just doesn't work.它只是行不通。 I was using d3 version 7, and a quick fix to it was to downgrade to version 6.7.0 , when it did support commonJS.我使用的是 d3 版本 7,当它支持 commonJS 时,快速修复它是降级到版本6.7.0

Now, I have to move on with this project, but I'll keep looking into it and into how I can use a recent version of d3 in my project.现在,我必须继续这个项目,但我会继续研究它以及如何在我的项目中使用最新版本的 d3。 If I do, I'll edit that answer!如果我这样做,我将编辑该答案!

Cheers干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在打字稿项目中导入节点模块。 ERR_REQUIRE_ESM - How to import node module in typescript project. ERR_REQUIRE_ESM 将 D3.js 7.0.0 与 Next.js 11.0.1 一起使用时,如何解决“[ERR_REQUIRE_ESM]:必须使用导入加载 ES 模块”的问题? - How do I resolve "[ERR_REQUIRE_ESM]: Must use import to load ES Module" when using D3.js 7.0.0 with Next.js 11.0.1? 升级 Node JS 和 ERR_REQUIRE_ESM:必须使用 import 加载 ES Module: 'inheritsloose.js' of Babel Runtime - Upgrading Node JS and ERR_REQUIRE_ESM: Must use import to load ES Module: 'inheritsloose.js' of Babel Runtime node-config 错误 [ERR_REQUIRE_ESM]: 必须使用 import 来加载 ES Module /default.js - node-config Error [ERR_REQUIRE_ESM]: Must use import to load ES Module /default.js 节点 JS (cpanel) 错误:我收到错误 [ERR_REQUIRE_ESM]:必须使用导入来加载 ES 模块 - Node JS (cpanel) Error: I'm getting an error [ERR_REQUIRE_ESM]: Must use import to load ES Module ERR_REQUIRE_ESM - TypeScript + nodemon + ts-node + node-fetch - ERR_REQUIRE_ESM - TypeScript + nodemon + ts-node + node-fetch 错误 [ERR_REQUIRE_ESM]:ES 模块的 require() - Vue 3 Typescript - Error [ERR_REQUIRE_ESM]: require() of ES Module - Vue 3 Typescript require('node-fetch') 给出 ERR_REQUIRE_ESM - require('node-fetch') gives ERR_REQUIRE_ESM Swagger-JSdoc 不适用于 Node.JS 14.15.4 错误“ERR_REQUIRE_ESM” - Swagger-JSdoc is not working on Node.JS 14.15.4 error 'ERR_REQUIRE_ESM' 错误 [ERR_REQUIRE_ESM]:如何在节点 12 中使用 es6 模块? - Error [ERR_REQUIRE_ESM]: How to use es6 modules in node 12?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM