235

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 {}

This results in an error:

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

While lib/index.js is correctly compiled:

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?

Estus Flask
  • 206,104
  • 70
  • 425
  • 565

22 Answers22

231

Try something like:

npm install tslib --save-dev

Or, to fix noob mistake (which I just made):

npm i

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

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Paul Lockwood
  • 4,283
  • 1
  • 24
  • 19
  • 3
    I'll never know why this isn't included in one of the _many_ angular dependencies :( – Ben Winding Aug 20 '19 at 04:29
  • 16
    "tslib is not a dependency of angular itself. But rather a dependency of the TypeScript compiler when you enable TypeScript importHelpers. Any library that is compiled using importHelpers should always have a direct dependency on tslib and not rely on other libraries to install it." https://github.com/angular/angular-cli/issues/13886#issuecomment-471927518 – Paul Lockwood Aug 23 '19 at 11:48
  • git clean -fxd command removed all useful files too. (.env and shared folders) – Aravin Mar 08 '21 at 05:38
  • 2
    @Aravin that's why you don't run any command you see on the internet – SIMMORSAL Feb 10 '23 at 09:14
224

The problem for me was that the editor was using a different TypeScript version than the project.

To fix that:

  1. Open Command Palette (Cmd+Shift+P on Mac. Focused file must be .ts or .tsx otherwise it won't show the option to change version)
  2. Select "TypeScript: Select TypeScript Version..."
  3. It shows VSCode's TS version and Workspace's (project) one, pick that one

Or click on the version number at the bottom bar if it shows in there:

zok
  • 6,065
  • 10
  • 43
  • 65
  • 11
    After updating to Ng12, with tslib already installed and getting `This syntax requires an imported helper but module 'tslib' cannot be found.` error, this easy solution was all I needed. PS, answer is a little ambiguous so to clarify you must pick: `Workspace's (project)` – Alfa Bravo Aug 23 '21 at 07:44
  • 4
    This should not be the accepted answer because the OP does not mention they are using VSCode. – Heretic Monkey Jan 26 '22 at 15:48
  • 1
    That solved my issue – J4N Apr 13 '22 at 11:14
  • Thanks, that helped. I changed from the built-in version 4.7.3 to the local node_modules same 4.7.3, and the errors went away ... – Arthur_J Jun 29 '22 at 11:39
  • Same here, this fixed my issue. VS Code were using 4.7.3 so I switched to Workspaces's 6.4.6 version and it fixed. – Dan Jul 07 '22 at 09:47
  • 3
    The worked for me, but I had to additionally use the "TypeScript: Reload project" command. – Bruce Jul 09 '22 at 05:02
61

Add below lines to tsconfig.json

"compilerOptions": {
    //...rest parameters

    "baseUrl": "./",
    "paths": {
      "tslib" : ["path/to/node_modules/tslib/tslib.d.ts"]
    },
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
23

In my case, tslib was already installed and i was seeing this error only for 1 component while everything else was working fine - build and functionality. I just restarted my vscode and it vanished. So, try that if the error is still there after you have done things which others have suggested.

Aman Gupta
  • 360
  • 2
  • 9
21

Just updated tslib to latest version, and problem had been fixed. I had installed 1.9.3, and updated to 1.10.0.

Pavel
  • 2,602
  • 1
  • 27
  • 34
21

In my case removing or setting the importHelpers compiler option to false resolved the issue.

{
  "compilerOptions": {
    ...
    "importHelpers": false, // Or remove this
    ...
  }
}
fortunee
  • 3,852
  • 2
  • 17
  • 29
9

As the reference states, module resolution is set to Node mode only for "modules": "commonjs", and is set to classic mode for "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

Since classic mode is unaware of node_modules, the compiler cannot resolve tslib module.

moduleResolution should be set explicitly for ES2015 modules:

...
"module": "es2015",
"moduleResolution": "node",
...
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
9

My error appeared to be sporadic but likely caused by editing a file in the "node_modules" folder.

I deleted

  • "node_modules" folder
  • "package-lock.json" file

Ran ... "npm install"

It is now working.

NOTE: I tried running "npm install' prior to deleting the files and that did not solve the issue.

user2032
  • 113
  • 1
  • 7
  • 2
    That's correct, it may be necessary to delete node_modules, `npm install` is not enough to forcibly reinstall. A specific damaged package can be reinstalled with `npm install packagename` but this doesn't work for all packages and won't fix all dependency problems. Deleting package-lock.json without good reasons is strongly discouraged and may bring new problems, unless you know that the problem propagated there. – Estus Flask Sep 07 '20 at 19:38
4

update the dependencies and devDependencies of tslib in package.json

{
   dependencies:{
     "tslib": "1.10.0",
   },
   devDependencies:{
     "tslib": "1.10.0",
   }
}
blazehub
  • 1,880
  • 19
  • 25
3

In my case the error Cannot find module 'tslib' was caused by special builder @nrwl/node:build in my Angular repository (I'm using nrwl/nx monorepo pattern with NodeJS server as one of the applications).

It didn't include tslib in the compilation output. Setting "externalDependencies": "none" in the production build configuration in angular.json solved the issue. Credit goes to @vincastl here: https://github.com/nrwl/nx/issues/2625

Posting it here as it was the first post I found trying to solve this issue, perhaps I'm not alone.

Alexey Grinko
  • 2,773
  • 22
  • 21
2
npm i -g tslib

This solved my problem. Without -g it didn't work for me.

WildHorse
  • 99
  • 1
  • 8
2

npm install

If you see this error the first time you open a new project or repository, you probably simply haven't installed the app's required modules yet.

In the directory of the app, run:

npm install

Tony Brasunas
  • 4,012
  • 3
  • 39
  • 51
2

I faced this issue with jest (+vuejs and a monorepo structure). All the app works fine, but unit tests doesn't.

So 2 things has to be done to make them work:

  1. in tsconfig.json:
    "compilerOptions": {
      "paths": {
        "tslib" : ["path/to/node_modules/tslib/tslib.d.ts"]
    }
  1. in jest.config.js:
    moduleNameMapper: {
      'tslib': '<rootDir>/path/to/node_modules/tslib/tslib.js'
    }
eXception
  • 1,307
  • 1
  • 7
  • 22
2

I was facing the same problem and it got resolved using following command!

npm install -D tslib@latest

2

(2023 solution)

As noted in the official tslib doc, tslib is a runtime library, therefore it must not be installed as a devDependencies with the -D flag. Run yarn workspace @scope/workspace add tslib or npm i tslib -w @scope/workspace to fix this error.

Son Nguyen
  • 2,991
  • 2
  • 19
  • 21
  • what's `@your-org/your-work-space` here? – Prasannjeet Singh May 15 '23 at 12:05
  • 1
    It's the name of a specific project in your monorepo (i.e. the `name` field in `package.json`). A monorepo has many projects (a.k.a NPM/YARN/PNPM workspaces) and each project has its own `package.json`. – Son Nguyen May 21 '23 at 14:51
2

SOLUTION:

>> npm install -g npm-check-updates

>> ncu -u tslib
    ...tslib  ^1.10.0  →  ^2.6.0
    [This command will update to the compatible version]

>> npm install
  • In my case, the OP issue popped up when I upgraded my Node version from 12+ to 16+ for my Angular 9 project.
  • By using the above commands, the tslib updated and the issue was fixed. :)

Just a note: In case if it doesn't work, kindly undo the tslib version and run "npm install" to revert to previous state. Because, other packages that are dependent on the older version might not be compatible. This too happened in my case.

Naveen Kumar V
  • 2,559
  • 2
  • 29
  • 43
1

You have to remove from "compilerOptions": { "importHelpers": true, } on tsconfig.json

Meriem Bader
  • 112
  • 2
  • 15
0

In your project , simply run

npm i tslib 

It contains all the typescript helper functions

superdwale
  • 37
  • 4
0

upgrading tslib version 2.00.0

in package.json it should be "tslib": "^2.0.0"

Chinthaka
  • 1
  • 1
0

I had a case, where the only fix was to add this manually in package.json :

"tslib": "^2.3.1",

and run npm i afterwards.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
0

add these lines to tsconfig.json file and inside the compilerOptions object:

"compilerOptions": {
    //...rest parameters

    "baseUrl": "./",
    "paths": {
      "tslib" : ["path/to/node_modules/tslib/tslib.d.ts"]
    },
-1

Add this below line in your .csproj file inside <PropertyGroup>:

<TypeScriptCompileBlocked>True</TypeScriptCompileBlocked>
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • 3
    How did you assume he is using .NET? – Daniel Stoyanoff Aug 12 '21 at 06:07
  • @DanielStoyanoff Most suggested solutions aren't applicable any way because the question contains a very specific problem described in [this answer](https://stackoverflow.com/a/52801821/3731501). I guess most posters just describe why they had this error. This may be useful for users that have a similar TS problem with a different cause, in this regard the post with C# setup doesn't look less useful as any other. – Estus Flask Nov 15 '21 at 10:51