TypeScript: TypeScript error “TS2354: This syntax requires an imported helper but module 'tslib' cannot be found”

TypeScript Version: 3.8.3

Search Terms: TS2354

Code

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "scripts": {
    "test": "tsc --noEmit --project ./tsconfig.json"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/argparse": "1.0.38",
    "argparse": "1.0.10",
    "tslib": "1.11.1",
    "typescript": "3.8.3"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "checkJs": true,
    "allowJs": true,
    "moduleResolution": "node",
    "target": "es2018",
    "module": "commonjs",
    "importHelpers": true,
    "lib": [
      "es2018"
    ]
  },
  "include": [
    "*.js"
  ],
  "exclude": [
    "node_modules"
  ]
}

index.js

'use strict';

const {ArgumentParser} = require('argparse');

Expected behavior:

Actual behavior:

I have reduced a problem with TypeScript to a simple example. When trying to run tsc, I get the following error message but tslib should be available.

$ tsc --noEmit --project ./tsconfig.json

index.js:3:8 - error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.

3 const {ArgumentParser} = require('argparse');
         ~~~~~~~~~~~~~~
Found 1 error.

Playground Link: https://codesandbox.io/s/quizzical-mclean-n9vvi?fontsize=14&hidenavigation=1&theme=dark

Related Issues:

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I was having this exact same issue and I figured it out!

Add to paths in tsconfig.json. And in your case, you will need to add “baseUrl” too.

{
  // ...rest of config
  "compilerOptions": {
    // ...rest of compiler options
    "baseUrl: ".",
    "paths": {
      "tslib" : ["path/to/node_modules/tslib/tslib.d.ts"]
    }
  }
}

I was having this exact same issue and I figured it out!

Add to paths in tsconfig.json. And in your case, you will need to add “baseUrl” too.

{
  // ...rest of config
  "compilerOptions": {
    // ...rest of compiler options
    "baseUrl: ".",
    "paths": {
      "tslib" : ["path/to/node_modules/tslib/tslib.d.ts"]
    }
  }
}

Thank you.

Seeing this error when opening Yarn PnP project in VIM. yarn add -D tslib@latest does not solves this. npm i tslib@latest does solves this, but it brings in node_modules directory to the project which is not what i want. In VS Code everything works fine because it handles PnP.

https://stackoverflow.com/questions/58329178/the-syntax-requires-an-imported-helper-named-spreadarrays

This error might happen if you have in your tsconfig.json file :

{ 
  "importHelpers": true,
  "target": "es5"
}

If for any reason you can’t upgrade to es6, setting importHelpers to false will make the error go away …

No matter what I do in the internet I still get the error.

  • I have upgraded tslib to ^1.10.0 and did npm i
  • I already have tsconfig.json “moduleResolution”: “node”, “module”: “esnext” so tried es2015 instead of esnext.
  • Tried restarting vscode after everychange.
  • Deleted and installed node modules from scratch. Funny thing I have spreadarray being used in many places of my application but only in one place where I copied mat-tree functionality I get below error.

This syntax requires an imported helper named ‘__spreadArray’ which does not exist in ‘tslib’. Consider upgrading your version of ‘tslib’.ts(2343)

I got the same __spreadArray error in my project. I npm i tslib to ^2.2.0 and the error is gone.

No matter what I do in the internet I still get the error.

  • I have upgraded tslib to ^1.10.0 and did npm i
  • I already have tsconfig.json “moduleResolution”: “node”, “module”: “esnext” so tried es2015 instead of esnext.
  • Tried restarting vscode after everychange.
  • Deleted and installed node modules from scratch. Funny thing I have spreadarray being used in many places of my application but only in one place where I copied mat-tree functionality I get below error.

This syntax requires an imported helper named ‘__spreadArray’ which does not exist in ‘tslib’. Consider upgrading your version of ‘tslib’.ts(2343)

Has anyone found a solution for this issue (with Yarn v2)?

For those using pnp, you need to resolve the pnp path within tsconfig in your “compilerOptions”:

const path = require("path");
const { resolveRequest } = require("pnpapi"); // built-in yarn pnp api

{
     "compilerOptions": {
         "baseUrl": ".",
         "paths": {
             "tslib": [resolveRequest("tslib", path.resolve("."))] // will resolve to ./.yarn/cache/...long-filename
         }
     }
}

I have a fix up for the commonjs require scenario, but I’m not disabling the checks when --noEmit is enabled since there is value in reporting errors that would occur when you emit, and you can pass --noEmit --importHelpers false on the command line if you don’t want the errors reported.

The reason for the issue in the OP is twofold. First, we were detecting the file is a module due to the require call (which is a signal to TypeScript the file is a module), and we only check for a valid tslib for modules. Second, we had a very weak check as to whether the __rest helper should be checked due to the destructuring assignment. #43166 addresses this weak check, which should solve the root cause of this issue.

Using yarn pnp this can be overcome by executing tsc with yarn pnpify.

yarn pnpify tsc --someoptions

I’m also seeing this error in JavaScript files when running with --checkJs and using object destructuring, even when targeting ES2020.