Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"This syntax requires an imported helper but module 'tslib' cannot be found" with ES2015 modules

Tags:

typescript

People also ask

How do you fix this syntax requires an imported helper but module Tslib Cannot be found?

To solve the error "This syntax requires an imported helper but module 'tslib' cannot be found", install tslib by running npm install -D tslib@latest and make sure your IDE is using the correct (workspace) TypeScript version. Copied!

What is Tslib in angular?

The tslib is a runtime library for Typescript. The version of this library is bound to the version of the TypeScript compiler used to compile a library. Peer dependencies do not accurately represent this relationship between the runtime and the compiler.

Is Tslib a dev dependency?

so, essentially, we can see that all the devDependencies are being removed(and note tslib ) was a dev dependency.


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!


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:


Add below lines to tsconfig.json

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

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

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


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",
...