You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Deno编写的npm包在create-react-app项目中正常运行?

问题描述

我正在尝试让用Deno编写的npm包能在基于create-react-app构建的React项目中使用。已经完成包的构建与发布,但在create-react-app项目中执行npm i my-package后,运行npm start时出现大量模块缺失错误:

Module not found: Error: Can't resolve 'fs' in ...
Module not found: Error: Can't resolve 'net' in ...
Module not found: Error: Can't resolve 'os' in ...
...

同时收到提示:

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

但我并没有主动使用这些Node.js核心API。

相关文件信息

Deno构建脚本

import { build, emptyDir } from "https://deno.land/x/dnt/mod.ts";

const outDir = "./dist";

await emptyDir(outDir);

await build({
  typeCheck: false,
  entryPoints: ["./client/src/index.ts"],
  outDir,
  shims: {
    undici: true,
    webSocket: true,
    timers: true,
    deno: true,
    crypto: true,
  },
  package: {
    name: "message-portal",
    version: Deno.args[0],
    description: "无需环境配置即可传递消息。",
    license: "MIT",
    repository: {
      type: "git",
      url: "git+https://github.com/uriva/portal.git",
    },
    bugs: {
      url: "https://github.com/uriva/portal/issues",
    },
  },
});

构建生成的package.json

{
  "module": "./esm/client/src/index.js",
  "main": "./script/client/src/index.js",
  "types": "./types/client/src/index.d.ts",
  "name": "message-portal",
  "version": "0.0.4",
  "description": "无需环境配置即可传递消息。",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/uriva/portal.git"
  },
  "bugs": {
    "url": "https://github.com/uriva/portal/issues"
  },
  "exports": {
    ".": {
      "import": {
        "types": "./types/client/src/index.d.ts",
        "default": "./esm/client/src/index.js"
      },
      "require": {
        "types": "./types/client/src/index.d.ts",
        "default": "./script/client/src/index.js"
      }
    }
  },
  "scripts": {
    "test": "node test_runner.js"
  },
  "dependencies": {
    "@deno/shim-crypto": "~0.3.1",
    "@deno/shim-timers": "~0.1.0",
    "undici": "^5.14.0",
    "ws": "^8.12.0"
  },
  "devDependencies": {
    "@types/node": "^18.11.9",
    "chalk": "^4.1.2",
    "@types/ws": "^8.5.4"
  }
}

内容的提问来源于stack exchange,提问作者Uri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 12:45:08