npm包ldap-pool安装时@types/ldapjs未引入问题求助
为什么安装
ldap-pool后@types/ldapjs没有自动安装? 你遇到的问题核心原因很明确:你把@types/ldapjs放在了devDependencies里,而非dependencies。
背后的依赖逻辑
npm对不同依赖字段的处理规则是关键:
devDependencies下的包仅在你开发ldap-pool模块本身时才会被安装——比如你跑测试、编译模块代码的场景。- 当其他项目通过
npm install ldap-pool将你的模块作为依赖引入时,npm默认只会安装dependencies中的包,devDependencies里的内容会直接被忽略。
而@types/ldapjs是ldapjs的TypeScript类型定义文件,使用ldap-pool的TypeScript项目必须依赖它才能正确识别ldapjs的类型,所以它属于依赖项目运行/编译时必需的包,不能放在devDependencies里。
解决方案
把@types/ldapjs从devDependencies移动到dependencies中,修改后的package.json示例如下:
{ "name": "ldap-pool", "version": "0.0.1011", "description": "LDAP client connection pool", "main": "index.js", "types": "index.d.ts", "typings": "index.d.ts", "scripts": { "test": "./@test.sh" }, "repository": { "type": "git", "url": "git+https://github.com/ORESoftware/ldap-pool.git" }, "keywords": [ "ldap", "pool", "client", "connection", "ldapjs" ], "author": "Olegzandr VD", "license": "ISC", "bugs": { "url": "https://github.com/ORESoftware/ldap-pool/issues" }, "homepage": "https://github.com/ORESoftware/ldap-pool#readme", "dependencies": { "chalk": "^1.1.3", "ldapjs": "^1.0.1", "@types/ldapjs": "^1.0.0" }, "devDependencies": { "@types/chalk": "^0.4.31", "@types/node": "^7.0.31" } }
完成修改后:
- 重新发布你的
ldap-pool模块到npm; - 其他项目重新执行
npm install ldap-pool,此时@types/ldapjs会被自动安装,TypeScript的红色波浪线问题也会随之解决。
可选替代方案(不推荐)
如果你更倾向于让用户自行管理类型依赖,可以把@types/ldapjs放在peerDependencies字段中,并在模块的README里明确告知用户需要手动安装@types/ldapjs。不过这种方式会增加用户的操作成本,所以优先推荐第一种方案。
内容的提问来源于stack exchange,提问作者user5047085
相关产品推荐
相关产品推荐

