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

Created on 15 Apr 2020  ·  5Comments  ·  Source: microsoft/TypeScript

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:

Bug Rescheduled

Most helpful comment

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

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

thanks gus!!!

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.

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: https://github.com/microsoft/TypeScript/issues/28289

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

Was this page helpful?
0 / 5 - 0 ratings