Last updated: Feb 29, 2024
Reading timeยท3 min
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:
// โ๏ธ 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); }
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.
npm install -D tslib@latest
If the error persists, your code editor might be running an older version of TypeScript.
If you use VSCode, you can:
CTRL + Shift + P
to open the command palette.typescript version
and click on
TypeScript: Select TypeScript version
.Use Workspace version
.target
to es6
or more recent versionIf 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.
{ "compilerOptions": { "importHelpers": true, "target": "es6", }, "include": ["src/**/*"], "exclude": ["node_modules", "src/**/*.spec.ts"] }
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.
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.
# ๐๏ธ (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.
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.
You can learn more about the related topics by checking out the following tutorials: