typescript ES2015 模块“此语法需要导入的帮助程序,但找不到模块‘tslib’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52801814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"This syntax requires an imported helper but module 'tslib' cannot be found" with ES2015 modules
提问by Estus Flask
I have demo project I'm about to compile to ES5 with ES2015 modules enabled and tslib
used for external TS helpers:
我有演示项目,我即将编译为 ES5,启用 ES2015 模块并tslib
用于外部 TS 助手:
package.json
包.json
{
"name": "foo",
"scripts": {
"build": "tsc"
},
"dependencies": {
"tslib": "^1.9.3"
},
"devDependencies": {
"typescript": "^3.1.3"
}
}
tsconfig.json
配置文件
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"outDir": "./lib",
"rootDir": "./src",
"importHelpers": true,
"strict": true,
"experimentalDecorators": true
}
}
src/index.ts
源代码/索引.ts
function a(target: any) {
return target;
}
@a
export class Foo {}
This results in an error:
这会导致错误:
src/index.ts:5:1 - error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
src/index.ts:5:1 - 错误 TS2354:此语法需要导入的帮助程序,但找不到模块“tslib”。
While lib/index.js
is correctly compiled:
虽然lib/index.js
正确编译:
import * as tslib_1 from "tslib";
function a(target) {
return target;
}
var Foo = /** @class */ (function () {
function Foo() {
}
Foo = tslib_1.__decorate([
a
], Foo);
return Foo;
}());
export { Foo };
How can this problem be solved?
如何解决这个问题?
回答by Paul Lockwood
Noob mistake (which I just made). Try:
菜鸟错误(我刚刚犯的)。尝试:
npm install tslib
or
或者
npm i
Personally before signing off on Friday I did a git clean -fxd
, but no npm i
so all the npm packages were missing. Doh!
在周五签字之前,我个人做了一个git clean -fxd
,但没有,npm i
所以所有的 npm 包都丢失了。哦!
回答by Pavel
Just updated tslib to latest version, and problem had been fixed. I had installed 1.9.3
, and updated to 1.10.0
.
刚刚将 tslib 更新到最新版本,问题已经解决。我已经安装1.9.3
并更新到1.10.0
.
回答by Estus Flask
As the referencestates, module resolution is set to Node mode only for "modules": "commonjs"
, and is set to classic mode for "modules": "es2015"
:
正如参考所述,模块分辨率仅针对 设置为节点模式,针对"modules": "commonjs"
设置为经典模式"modules": "es2015"
:
There are two possible module resolution strategies: Node and Classic. You can use the --moduleResolution flag to specify the module resolution strategy. If not specified, the default is Classic for --module AMD | System | ES2015 or Node otherwise
有两种可能的模块解析策略:Node 和 Classic。您可以使用 --moduleResolution 标志来指定模块解析策略。如果未指定,默认为 Classic for --module AMD | 系统 | ES2015 或 Node 否则
Since classic mode is unaware of node_modules
, the compiler cannot resolve tslib
module.
由于经典模式不知道node_modules
,编译器无法解析tslib
模块。
moduleResolution
should be set explicitly for ES2015 modules:
moduleResolution
应该为 ES2015 模块显式设置:
...
"module": "es2015",
"moduleResolution": "node",
...
回答by blazehub
update the dependencies and devDependencies of tslib in package.json
更新 package.json 中 tslib 的依赖项和 devDependencies
{
dependencies:{
"tslib": "1.10.0",
},
devDependencies:{
"tslib": "1.10.0",
}
}
回答by Eugine Santhana Sagayam Manuel
Add this below line in your .csproj file inside <PropertyGroup>
:
在您的 .csproj 文件中添加以下行<PropertyGroup>
:
<TypeScriptCompileBlocked>True</TypeScriptCompileBlocked>