Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
doberkofler opened this issue Apr 15, 2020 · 16 comments · Fixed by #43166
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@doberkofler
Copy link

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:

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Apr 17, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.0 milestone Apr 17, 2020
@jordanjordanb1
Copy link

jordanjordanb1 commented May 18, 2020

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"]
    }
  }
}

@chiuan
Copy link

chiuan commented Jul 5, 2020

thanks gus!!!

@dzintars
Copy link

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.

@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Aug 31, 2020
@maneetgoyal
Copy link

maneetgoyal commented Oct 15, 2020

Facing similar issue while migrating from Yarn 1.x to Yarn 2.x (which uses pnp).

Edit:

This might help someone who wants to dig in more: #28289

@emmenko
Copy link

emmenko commented Oct 15, 2020

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

@rbuckton
Copy link
Member

Seems like our checker is incorrectly checking for lib helpers in cases where they're not needed. We shouldn't need to check for them when using --noEmit or when checking CommonJS import syntax that just uses require.

@mihe
Copy link

mihe commented Dec 23, 2020

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

@DanCouper
Copy link

DanCouper commented Jan 6, 2021

Ditto on object destructuring triggering the error. I can't use the paths option to fix this as I'm using PnP (I don't have a node_modules to point to). Is there a solution to get it working for the minute apart from either:

  1. removing object destructuring from the code that is triggering the error (this is what I'm currently doing).
  2. unplugging tslib then using a path to that (which seems unfeasibly fragile as it unplugs to a folder with a fully-qualified name like tslib-npm-2.0.1-26f70961f9).

(nb installing tslib via NPM isn't really an option for me here)

EDIT: further information. I can now make the error go away by either disabling incremental compilation, or deleting the tsconfig.tsbuildinfo files between compiles. I don't understand why this would either be triggering or fixing the bug, but it's definitely fixing the problem for me. I'm using Rollup with the typescript2 plugin & Yarn 2 monorepo with PnP, compiling to ESNext. So if I leave incremental on, add a file, then don't delete the tsbuildinfo file, the bug occurs (triggered by using object spread in a function argument, spread works elsewhere). Once I delete that tsbuildinfo file and recompile, all works until next time I add a file.

@janpauldahlke
Copy link

just came accross this. can anyone provide cleaner solution, than adding ts-with npm? much appriciated.

@elpddev
Copy link

elpddev commented Feb 2, 2021

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

yarn pnpify tsc --someoptions

@rbuckton rbuckton mentioned this issue Mar 9, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Mar 9, 2021
@rbuckton
Copy link
Member

rbuckton commented Mar 9, 2021

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.

@slst19
Copy link

slst19 commented May 7, 2021

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)

@evollhhan
Copy link

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.

@RodrigoAB93
Copy link

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.

@klngrs
Copy link

klngrs commented Jan 18, 2022

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
         }
     }
}

@zrysmt
Copy link

zrysmt commented May 19, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.