Failed to resolve entry for package "js-demo-tools". The package may have ......

背景:在我自己开发的npm包,上传到了npm之后,测试的时候使用vite框架进行导入这个包报的错误

json 复制代码
{
  "name": "xxx-demo",
  "version": "1.0.0",
  "description": "this is learn npm package",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "build": "rollup -c"
  },
  "files": [
    "/dist"
  ],
  "keywords": [],
  "author": "",
  "license": "ISC",
  "packageManager": "pnpm@10.14.0",
  "devDependencies": {
    "rollup": "^4.46.4",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-plugin-terser": "^7.0.2",
    "rollup-plugin-typescript2": "^0.36.0"
  }
}
  1. main 字段指向错误 :"main": "index.js" 指向根目录的 index.js,但我的 files 配置只包含 /dist 文件夹,所以发布到 npm 的包中根本没有根目录的 index.js文件。

  2. 缺少 ES 模块入口配置 :虽然你设置了 "type": "module",但没有配置 module 字段或现代的 exports 字段来指定 ES 模块的入口点。

  3. Vite 无法找到正确的入口:Vite 作为现代构建工具,优先查找 ES 模块入口,但你的配置没有提供正确的路径。

推荐 直接使用 exports 字段

json 复制代码
{
  "name": "xxx-demo",
  "version": "1.0.0",
  "description": "this is learn npm package",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "build": "rollup -c"
  },
  "files": [
    "/dist"
  ],
  "exports": {
    ".": {
      "import": "./dist/index.js",
      "require": "./dist/index.cjs"
    }
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "packageManager": "pnpm@10.14.0",
  "devDependencies": {
    "rollup": "^4.46.4",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-plugin-terser": "^7.0.2",
    "rollup-plugin-typescript2": "^0.36.0"
  }
}

为什么推荐呢?

  1. 现代标准 :exports 字段是 Node.js 和现代打包工具的标准
  2. 明确的入口点:清楚地指定了不同模块系统的入口
  3. 更好的兼容性:同时支持 ES 模块和 CommonJS
  4. Vite 友好 :Vite 会优先查找 exports.import 字段
相关推荐
陈随易24 分钟前
Postman 被曝自动往 Claude Code 和 Codex 装插件
前端·后端·程序员
Csvn3 小时前
性能监控
前端
Csvn4 小时前
构建工具
前端
FRing034 小时前
vue2干货总结3(computed计算属性、watch监听器)
开发语言·前端·vue.js·vue2
kyriewen7 小时前
Skills 榜前 10 我全装了:只有 3 个能直接用
前端·javascript·程序员
IT_陈寒7 小时前
当Java的hashCode遇上Lombok,我的缓存被全清了
前端·人工智能·后端
工控丫丫7 小时前
帆软看板中经常使用的公式函数及经常搭的组合
前端·javascript·数据库
jerrywus8 小时前
我用 GPT-6.1-sol 做了台会讲解的 3D 售货机:60 罐饮料,一口没喝上
前端·agent·claude
孟陬9 小时前
Foobar 技术周刊#159:当看不懂代码时,如何高效排障调试。脏页陷阱:一条只读查询,写入数 TB 数据。推测解码:大模型加速利器
前端·人工智能·后端
Zhao-Jichao9 小时前
AI Prompting for Everyone 课程笔记 - P17 图像生成 Imaging Generation
前端