如何让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
相关产品推荐
相关产品推荐

