如何避免“未找到模块":错误:引用外部打字稿文件时无法解决"tslib"错误? [英] How to avoid "Module not found: Error: Can't resolve 'tslib'"-error when referencing an external typescript file?
问题描述
我有一个客户端/服务器应用程序,其中客户端是一个有角度的应用程序.服务器确实为其提供的API提供了几个typescript定义.这些定义是从项目外部文件夹加载的.到目前为止,这工作正常,但是现在我从Angular 7升级到Angular 8(并且还将Typescript从3.1版升级到3.5),并且当尝试从定义中使用非静态枚举时,现在会产生此错误:
I have a client/server application, where the client is an angular-app. The server does provide a couple of typescript-definitions for the API it provides. These definitions are loaded from a project-external folder. So far this worked fine, however now I am upgrading from Angular 7 to Angular 8 (and with that also upgrading Typescript from Version 3.1 to 3.5) and when trying to use a non-static enum from the definitions, this now produces this error:
未找到模块:错误:无法解析'/path/to/server/definitions'中的'tslib'
Module not found: Error: Can't resolve 'tslib' in '/path/to/server/definitions'
当api.ts
文件位于client-dir中时,一切正常.
When the api.ts
-file is located within the client-dir, then everything works just fine.
为什么会这样,我该怎么办?
这是一个快速脚本,可创建一个最小的示例来重现:
Here is a quick script to create a minimal example to reproduce:
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
或者,或者手动创建示例:
Or, alternatively, to create an example manually:
- 创建一个新的角度项目
- 在文件系统中的某个位置创建内容为
export enum ApiSample { X = "X" }
的文件"api.ts"
- 在"tsconfig.json"中,将
{"paths":{"api":["/path/to/server/api"]}
添加到compilerOptions
- 从角度项目的任何文件访问
ApiSample.X
- 运行
npm start
- Create a new angular project
- Create somewhere in the filesystem a file "api.ts" with the content
export enum ApiSample { X = "X" }
- In the "tsconfig.json" Add
{"paths":{"api":["/path/to/server/api"]}
to thecompilerOptions
- Access the
ApiSample.X
from any file of the angular project - Run
npm start
到目前为止的发现
- 迁移者在tsconfig.json中添加了
"importHelpers": true
.这就是为什么它以前起作用而现在不再起作用.在tsconfig.json中指定"importHelpers": false
可以解决此问题,但是当然存在此选项是有原因的,因此这不是最终解决方案 - 明确指定
"tslib": ["node_modules/tslib"]
可解决此问题.这可能是最终的解决方案,尽管了解确切发生的情况以及在何处记录下来会很有趣. - The migrator added
"importHelpers": true
in tsconfig.json. That is why it worked before and is not working anymore now. Specifying"importHelpers": false
in tsconfig.json fixes the issue, but of course this option is there for a reason, so that is no final solution - Explicitly specifying
"tslib": ["node_modules/tslib"]
fixes the problem. This may be a final solution although it would be interesting to know what is exactly happening and where this is documented.
Findings so far
推荐答案
Angular 8& 9
因此对于Angular 8和Angular 9,似乎在tsconfig.json中将"tslib": ["node_modules/tslib"]
添加到compilerOptions/paths
确实可以解决问题.
Angular 8&9
So for Angular 8 and 9 it seem that adding "tslib": ["node_modules/tslib"]
to compilerOptions/paths
in the tsconfig.json does solve the problem.
在Angular 10中,问题完全消失了,不再需要任何解决方法.
In Angular 10 the problem disappeared altogether, no workaround is needed anymore.
这篇关于如何避免“未找到模块":错误:引用外部打字稿文件时无法解决"tslib"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!