【问题标题】:index.d.ts file not published to NPMindex.d.ts 文件未发布到 NPM
【发布时间】:2021-08-04 00:16:21
【问题描述】:

我在 Typescript 中创建了一个包并在 NPM 上发布了它。我在这个包项目的tsconfig.json 中设置了declaration: true。当我构建这个项目时,TS 编译器按预期生成 .d.ts 文件,但是,这些文件没有在 NPM 上发布,因此,当我在其他项目中安装这个包时,我收到错误,@987654323 @

这是我的 package.json 文件:

{
  "name": "@Organisation/lib",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "clean": "rm -rf ./dist",
    "build": "npm run clean && npx tsc",
    "pub": "git add . && git commit -m \"Changes\" && git push && npm version patch && npm run build && npm publish --access public"
  },
  "keywords": [],
  "author": "Author",
  "license": "ISC",
  "devDependencies": {
    //removed
  },
  "dependencies": {
    //removed
  }
}

这是我的 tsconfig.json 文件:

{
    "compilerOptions": {
      "target": "es6",                                /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
      "module": "commonjs",                           /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
      "declaration": true,                         /* Generates corresponding '.d.ts' file. */
      "sourceMap": true,                           /* Generates corresponding '.map' file. */
      "outDir": "dist",                              /* Redirect output structure to the directory. */
      "strict": true,                                 /* Enable all strict type-checking options. */
      "noImplicitAny": true,                       /* Raise error on expressions and declarations with an implied 'any' type. */
      "moduleResolution": "node",                  /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
      "baseUrl": "./",                             /* Base directory to resolve non-absolute module names. */
      "paths": {
        "*": ["*"]
      },                                 /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
      "allowSyntheticDefaultImports": true,        /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
      "esModuleInterop": true,                        /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      "skipLibCheck": true,                           /* Skip type checking of declaration files. */
      "forceConsistentCasingInFileNames": true        /* Disallow inconsistently-cased references to the same file. */
    },
    "include": [
      "src/**/*"
    ]
  }

这是我的 .gitignore 文件:

lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.swp

pids
logs
results
tmp

# Build
public/css/main.css

# Coverage reports
coverage

# API keys and secrets
.env

# Dependency directory
node_modules
bower_components

# Editors
.vscode
.idea
*.iml

# OS metadata
.DS_Store
Thumbs.db

# Ignore built ts files
dist/**/*

# ignore yarn.lock
yarn.lock

**.log.**

注意:即使 dist 包含在 .gitignore 中,文件 dist/index.js 也会发布在 NPM 上,但没有 .d.ts 文件。

目录结构是这样的:

my-package/
├─ node_modules/
├─ dist/
│  ├─ models/
│  ├─ index.d.ts
│  ├─ index.js
│  ├─ index.js.map
├─ src/
│  ├─ models
│  ├─ index.ts
├─ .gitignore
├─ package.json
├─ tsconfig.json

【问题讨论】:

  • 添加一个 .npmignore 文件,其中包含您不想发布到 npm 的文件。这将覆盖 .gitignore 的设置。我不知道你是如何让 dist/index.js 发布的。
  • 我也在包项目根目录中做了npx npm-packlist,这里包含dist/index.js,但没有其他.d.ts文件。

标签: javascript typescript npm


【解决方案1】:

你需要使用files键,这样只有files数组中的东西会被发布到npm,你不需要.npmignore

示例:

  "main": "dist/cjs/index.js",
  "module": "dist/esm/untilted.esm.js",
  "unpkg": "dist/unpkg/untilted.js",
  "types": "dist/types/",
  "files": [
    "dist",
    "src"
  ],

在本例中,dist 目录保存所有编译文件,src 保存源代码(源映射需要)。这就是将要发布到 npm 的所有内容以及一些始终包含的文件(package.json、README、LICENCE 等...)

【讨论】:

  • 这样,tsconfig.json 不包括在内,这样可以吗?另外,请您指出我当前配置中的不正确之处吗?
  • 您不需要发布tsconfig.json,如果您阅读了我提供的链接,您将了解到main 字段始终包含在发布的包中,并且您的main 点到dist/index.js
  • 感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 2018-04-30
  • 2019-08-08
  • 1970-01-01
  • 2021-02-11
  • 2021-02-05
  • 2018-12-04
  • 2021-02-08
  • 1970-01-01
相关资源
最近更新 更多