244

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?

23 Answers 23

252

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 (Note: this assumes you have npm installed the packages in the workspace) select typescript version Or click on the version number at the bottom bar if it shows in there:

7
  • 12
    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, 2021 at 7:44
  • 4
    This should not be the accepted answer because the OP does not mention they are using VSCode. Jan 26, 2022 at 15:48
  • 1
    That solved my issue
    – J4N
    Apr 13, 2022 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, 2022 at 11:39
  • 3
    The worked for me, but I had to additionally use the "TypeScript: Reload project" command.
    – Bruce
    Jul 9, 2022 at 5:02
240

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!

4
  • 3
    I'll never know why this isn't included in one of the many angular dependencies :( Aug 20, 2019 at 4: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." github.com/angular/angular-cli/issues/… Aug 23, 2019 at 11:48
  • git clean -fxd command removed all useful files too. (.env and shared folders)
    – Aravin
    Mar 8, 2021 at 5:38
  • 2
    @Aravin that's why you don't run any command you see on the internet
    – SIMMORSAL
    Feb 10 at 9:14
68

Add below lines to tsconfig.json

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

    "baseUrl": "./",
    "paths": {
      "tslib" : ["path/to/node_modules/tslib/tslib.d.ts"]
    },
1
  • This seems to have resolved the issue.
    – Ali Celebi
    Oct 18, 2022 at 11:43
25

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

{
  "compilerOptions": {
    ...
    "importHelpers": false, // Or remove this
    ...
  }
}
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.

2
  • 1
    It really helps sometimes to restart IDE or devserver in case there are obscure TS errors that aren't reproducible and shouldn't be there. Jan 26, 2022 at 5:36
  • 2
    Yup, got the same problem, restarting VSCode fixed it for me. :) Sep 7, 2022 at 1:14
21

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

0
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",
...
1
  • 1
    I also had to set "target" as well. May 17, 2022 at 17:44
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.

1
  • 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. Sep 7, 2020 at 19:38
5

update the dependencies and devDependencies of tslib in package.json

{
   dependencies:{
     "tslib": "1.10.0",
   },
   devDependencies:{
     "tslib": "1.10.0",
   }
}
2
  • 9
    Imo adding only to dependencies should be enough, they're installed in development mode too.
    – Zmey
    Jun 3, 2020 at 14:15
  • @Zmey it only works in devDependencies for me. Jul 7, 2022 at 8:05
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.

1
  • Thanks. This helped me. I searched the error and nothing helped until I added "nx" in my search and found this. Mar 5, 2021 at 8:31
3

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

3

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

npm install -D tslib@latest

1
  • I feel like this doesn't contribute anything over the top answer.
    – starball
    Sep 3, 2022 at 21:21
3

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

3

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'
    }
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.

2
  • what's @your-org/your-work-space here? May 15 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 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.

2

I was facing the same issue in a NX Monorepo, specifically with an express node project bundled with webpack created using @nrwl/node/@nx/node. If I just build the app for production it would run well on my local machine, but when I was trying to build into a docker container, it always returned me this error:

node:internal/modules/cjs/loader:1080
  throw err;
  ^

Error: Cannot find module 'tslib'
Require stack:
- /app/backend/main.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Array.<anonymous> (/app/backend/main.js:8:18)
    at __webpack_require__ (/app/backend/main.js:37:41)
    at /app/backend/main.js:51:17
    at /app/backend/main.js:63:3
    at Object.<anonymous> (/app/backend/main.js:68:12)
    at Module._compile (node:internal/modules/cjs/loader:1256:14) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/app/backend/main.js' ]
}

I solved it by adding "externalDependencies": "none" in the project.json file:

"targets": {
    "build": {
      ...
      "options": {
        ...
        "externalDependencies": "none"
      },

The NX documentation says:

externalDependencies: Dependencies to keep external to the bundle. (all (default), none, or an array of module names)

I hope it helps anybody struggling with the same problem.

1
npm i -g tslib

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

0

In your project , simply run

npm i tslib 

It contains all the typescript helper functions

1
0

upgrading tslib version 2.00.0

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

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.

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>
2
  • 3
    How did you assume he is using .NET? Aug 12, 2021 at 6:07
  • @DanielStoyanoff Most suggested solutions aren't applicable any way because the question contains a very specific problem described in this answer. 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. Nov 15, 2021 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.