This syntax requires an imported helper but module 'tslib' cannot be found

avatar

Borislav Hadzhiev

Last updated: Mar 24, 2022

banner

Photo from Unsplash

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.

Here is an example of how the error occurs:

index.ts
// ⛔️ Error: This syntax requires an imported helper but module 'tslib' cannot be found @sealed export class BugReport { type = 'report'; title: string; constructor(t: string) { this.title = t; } } function sealed(constructor: any) { Object.seal(constructor); Object.seal(constructor.prototype); }

First, make sure you have tslib installed by opening your terminal in the root directory of your project and running:

shell
npm install -D tslib@latest
If the error is not resolved, your code editor might be running an older version of TypeScript.

If you use VSCode, you can press CTRL + Shift + P to open the command palette and type in typescript version and click on TypeScript: Select TypeScript version and then click Use Workspace version.

Restart your IDE and your development server if necessary. VSCode often needs a reboot even after the error is resolved.

If you still get the error, open your tsconfig.json file and make sure your target option is set to es6 or more recent.

Note that you may also have to set the option in your tsconfig.app.json file if using Angular, or whichever config file is used in your build process.

tsconfig.json
{ "compilerOptions": { "importHelpers": true, "target": "es6", }, "include": ["src/**/*"], "exclude": ["node_modules", "src/**/*.spec.ts"] }

The target option changes which JavaScript features are downleveled and which are left intact.

If you aren't trying to use tslib helper functions, remove the importHelpers option if you have it enabled in your tsconfig.json file.

If the error is not resolved, try to delete your node_modules and package-lock.json files, re-run npm install and restart your IDE.

shell
rm -rf node_modules package-lock.json npm install

Restart your IDE if the error persists.

If you use the include or files options, make sure the file you working in is included in your project and is being tracked by TypeScript.

If your project uses the exclude option, make sure your exclude array does not exclude the file in which you're working.

The exclude option changes what the include option finds, effectively filtering out some folders or files from compilation.

Conclusion #

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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.