![](/img/trans.png)
[英]Module not found: Error: Can't resolve ... when using external JS library
[英]How to avoid “Module not found: Error: Can't resolve 'tslib'”-error when referencing an external typescript file?
我有一个客户端/服务器应用程序,其中客户端是一个角度应用程序。 服务器确实为它提供的 API 提供了几个打字稿定义。 这些定义是从项目外部文件夹加载的。 到目前为止,这工作正常,但是现在我正在从 Angular 7 升级到 Angular 8 (并且同时升级 Typescript 会产生从版本 3.1 到现在使用此定义的非静态错误:3.5)
找不到模块:错误:无法解析“/path/to/server/definitions”中的“tslib”
当api.ts
文件位于客户端目录中时,一切正常。
为什么会发生这种情况,我能做些什么?
这是一个快速脚本,用于创建一个最小的示例来重现:
mkdir project
cd project
ng new client --minimal=true --skipGit=true --routing=false --style=css
mkdir server
echo 'export enum ApiSample { X = "X" }' > server/api.ts
sed -i 's/"compilerOptions": {/"compilerOptions": {"paths":{"api":["..\/server\/api"]},/' client/tsconfig.json
sed -i -e "1iimport { ApiSample } from 'api';" -e "s/title = 'client';/title = 'client' + ApiSample.X;/" client/src/app/app.component.ts
cd client
npm start
或者,手动创建示例:
export enum ApiSample { X = "X" }
{"paths":{"api":["/path/to/server/api"]}
到compilerOptions
ApiSample.X
npm start
"importHelpers": true
。 这就是为什么它以前有效而现在不再有效的原因。 在 tsconfig.json 中指定"importHelpers": false
解决了这个问题,但是这个选项当然是有原因的,所以这不是最终的解决方案"tslib": ["node_modules/tslib"]
解决了这个问题。 这可能是最终的解决方案,尽管了解到底发生了什么以及记录在哪里会很有趣。 因此,对于 Angular 8 和 9,似乎将"tslib": ["node_modules/tslib"]
添加到 tsconfig.json 中的compilerOptions/paths
确实可以解决问题。
在 Angular 10 中,问题完全消失了,不再需要解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.