安装的 NPM 包缺少 TypeScript 编译的 JS 文件和声明文件

Installed NPM Package is Missing TypeScript Compiled JS Files and Declaration Files

我已经在 NPM 上发布了一个 TypeScript 库。在 GitHub 上,dist (Link to Repository Folder) 目录包含所有已编译的 JavaScript 和 d.ts 文件。然而,当 运行 npm i <my_package> 的结果是一个包含 dist 文件夹且仅包含 index.js 的模块时,src 文件夹仍然与 TypeScript 文件一起存在。 dist/src 文件夹连同它的 JavaScript 和 d.ts 文件在安装包中找不到,因此我的模块在 Vanilla JavaScript 项目中无法识别或键入。

图书馆tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
  },
  "exclude": [
    "node_modules/",
    "dist/"
  ]
}

图书馆package.json

{
  "name": "easy-trivia",
  "version": "2.0.3",
  "description": "A wrapper for the Open Trivia Database API. Built with TypeScript, works with VanillaJS.",
  "keywords": [
    "trivia",
    "games",
    "fun",
    "api",
    "easy",
    "typescript",
    "small",
    "quiz",
    "opentriviadatabase",
    "opentdb",
    "opentriviadb"
  ],
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "build": "tsc",
    "prettier": "npm run build && prettier -w src/",
    "prepublish": "npm run prep",
    "exec": "npm run build && node .",
    "test": "npm run build && npx jest",
    "prep": "npm run build && npm run test && npm run prettier"
  },
  "files": [
    "src",
    "typings"
  ],
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Elitezen/easy-trivia.git"
  },
  "author": "Elitezen",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/Elitezen/easy-trivia/issues"
  },
  "homepage": "https://github.com/Elitezen/easy-trivia#readme",
  "devDependencies": {
    "@types/jest": "^27.0.3",
    "@types/node": "^16.11.7",
    "jest": "^27.5.1",
    "nodemon": "^2.0.12",
    "prettier": "2.4.1",
    "ts-jest": "^27.1.3"
  },
  "engines": {
    "node": ">=14.0.0",
    "npm": ">=7.0.0"
  }
}

要发布类型,您需要在 package.jsonfiles 字段中明确包含 dist 目录。仅在 types 中指定文件实际上不会包含它。对于 main 这是不同的,因为它会自动包含指定的文件。根据 npm package.json docs:

Certain files are always included, regardless of settings:

  • package.json
  • README
  • LICENSE / LICENCE
  • The file in the "main" field

要轻松验证包含哪些文件,npm pack --dry-run 很方便。