【发布时间】: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