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

avatar
Borislav Hadzhiev

Last updated: Feb 29, 2024
3 min

banner

# 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); }
The code for this article is available on GitHub

# Install the latest version of tslib

First, make sure you have tslib installed by opening your terminal in the root directory of your project and then run the following command.

shell
npm install -D tslib@latest

error solved after installing latest version of tslib

# Make sure your IDE uses the correct version of TypeScript

If the error persists, your code editor might be running an older version of TypeScript.

If you use VSCode, you can:

  1. Press CTRL + Shift + P to open the command palette.
  2. Type in typescript version and click on TypeScript: Select TypeScript version.
  3. 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.

# Set target to es6 or more recent version

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 code for this article is available on GitHub

The target option changes which JavaScript features are down-leveled 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.

# Delete your node_modules and reinstall your dependencies

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
# ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force npm install

Restart your IDE and development server if the error persists.

# Make sure the file is included in your project

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

If your project uses the exclude option, make sure your exclude array doesn't 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.

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev