[英]My app can't find my TypeScript package downloaded from the NPM registry (module not found)
我在打包我在npm 注册表上发布的TypeScript 项目时遇到问题。 我是打包模块供其他人使用的新手,所以我可能只是做错了什么。
这些是package.json中似乎相关的部分:
{
"name": "robotnik-bot",
"version": "0.1.0-4",
"description": "Friendly neighborhood Discord bot",
"main": "out/index",
"types": "out/index.d.ts",
"typings:": "out/index.d.ts",
"scripts": {
"build": "tsc",
"test": "mocha -r ts-node/register tests/**/*.test.ts",
"coverage": "nyc -r lcov -e .ts -x \"*.test.ts\" npm run test",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"prepublish": "tsc"
},
这是tsconfig.json :
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "out",
"sourceRoot" : "src",
"sourceMap": true,
"declaration": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"moduleResolution": "node",
"typeRoots": [
"node_modules/@types"
]
},
"exclude": [
"out",
"nodule_modules",
"tests"
],
"lib": ["es2015"],
}
在我的依赖项目中,我添加了 package 作为依赖项并尝试使用它:
"dependencies": {
"robotnik-bot": "^0.1.0-4"
},
"devDependencies": {
"typescript": "^3.8.2"
},
"scripts": {
"start" : "tsc && node dist/index.js"
}
import { Builder } from 'robotnik-bot';
const bot = new Builder().build();
但是依赖项目找不到模块。 当我检查nodue_modules
时,它似乎刚刚将我的项目文件结构与 TypeScript 源打包在一起; 不应该有一些编译的Javascript吗?
应该有编译文件。 如果没有,您要么发布了一个干净的工作目录(没有已编译文件),要么您的打包配置为不包含已编译文件。
您可以使用npm publish --dry-run
查看将包含哪些内容。
请注意文档中的以下内容:
将 package 发布到注册表,以便可以按名称安装。 如果不存在本地
.gitignore
或.npmignore
文件,则包括 package 目录中的所有文件。 如果两个文件都存在并且一个文件被.gitignore
而不是被.npmignore
忽略,那么它将被包含
您的文件似乎通过.gitignore
被排除在外,这是有道理的。 您可以使用package.json
中的files
属性将它们添加回来,例如
"files": [
"out"
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.