“此语法需要导入的帮助程序,但找不到模块 'tslib'"带有 ES2015 模块

“此语法需要导入的帮助程序,但找不到模块 'tslib'

问题描述:

我有一个演示项目,我将要编译为 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!