“此语法需要导入的帮助程序,但找不到模块 'tslib'"带有 ES2015 模块 [英] "This syntax requires an imported helper but module 'tslib' cannot be found" with ES2015 modules

查看:219
本文介绍了“此语法需要导入的帮助程序,但找不到模块 'tslib'"带有 ES2015 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个演示项目,我将要编译为 ES5,启用 ES2015 模块并且 tslib 用于外部 TS 助手:

I have demo project I'm about to compile to ES5 with ES2015 modules enabled and tslib used for external TS helpers:

package.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

function a(target: any) {
    return target;
}

@a
export class Foo {}

这会导致错误:

src/index.ts:5:1 - 错误 TS2354:此语法需要导入的帮助程序,但找不到模块tslib".

src/index.ts:5:1 - error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.

虽然 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 };

如何解决这个问题?

推荐答案

菜鸟错误(我刚刚犯的错误).试试:

Noob mistake (which I just made). Try:

npm install tslib

npm i

在周五结束之前,我个人做了一个 git clean -fxd,但是没有 npm i 所以所有的 npm 包都丢失了.呸!

Personally before signing off on Friday I did a git clean -fxd, but no npm i so all the npm packages were missing. Doh!

这篇关于“此语法需要导入的帮助程序,但找不到模块 'tslib'"带有 ES2015 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆