rollup-plugin-typescript2: `TS2354 This syntax requires an imported helper but module 'tslib' cannot be found.`

I see that the code has some references to tslib and importHelpers so I assume this should work transparently. If not, I’ll be happy to know what is missing.

Here is how to reproduce:

Installed packages:

$ npm ls --depth=0
├── resolve@1.3.2
├── rollup@0.41.6
├── rollup-plugin-typescript2@0.4.0
├── tslib@1.6.0
└── typescript@2.2.2

tsconfig.json:

{
    "compilerOptions": {
        "target": "es5"
    }
}

rollup.config.js:

import typescript from 'rollup-plugin-typescript2';

export default {
	entry: './main.ts',

	plugins: [
		typescript()
	]
}

main.ts:

import {Foo} from './module';

console.log("HERE" + Foo);

And module.ts:

export class Foo {}

export class Bar extends Foo {}

When running rollup as follows:

./node_modules/.bin/rollup -c rollup.config.js

I get this error:

🚨   rpt2: module.ts (3,18): semantic error TS2354 This syntax requires an imported helper but module 'tslib' cannot be found.
module.ts

I think this is because the extends syntax requires an __extends helper from tslib, but typescript can’t find tslib.

Expected result is that the required helpers become part of the bundle.

Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 5
  • Comments: 19 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Looks like you need "moduleResolution": "node" in tsconfig, otherwise typescript can’t find tslib in node_modules.

Not sure if that could pose problems on non-node setups, but I haven’t heard anything to that effect yet.

@hueitan i faced the same issue while already having "moduleResolution": "node". Fixed it by adding "tslib": "^1.10.0" to my devDependencies.

This was fixed with this

@hueitan i faced the same issue while already having "moduleResolution": "node". Fixed it by adding "tslib": "^1.10.0" to my devDependencies.

Fixed! Mine was in regular dependencies.

Not sure anyone else facing the same problem as mine.

I was having this problem https://github.com/rollup/rollup-plugin-typescript/issues/109, therefore I switch to this repo, but then I’m getting this tslib error.

as the tsconfig is already "moduleResolution": "node"

in my case i am using yarn. Deleting the yarn.lock and reinstalling fresh, i do not need an explicit mention of tslib in the package.json

I can reproduce it in the following repo:

https://github.com/giniedp/tweak-ui

git clone git@github.com:giniedp/tweak-ui.git
cd tweak-ui
git checkout v0.1.0

now edit the package.json and remove tslib. Then do

yarn install
yarn run build

you should run into

This syntax requires an imported helper named '__spreadArrays', but module 'tslib' has no exported member '__spreadArrays'

now delete the yarn.lock and then

yarn install
yarn run build

runs fine.

Fixed it by adding "tslib": "^1.10.0" to my dependencies.

npm i tslib -D

worked for me too

adding tslib as the dependencies work to me as well. although not the pretty solution

None of the proposed solutions worked for me. Upgrading rollup-plugin-typescript2 did the trick.

npm i rollup-plugin-typescript2@0.27.1

It turns out rollup was using tslib@1.9.3 even when I had ^1.10.0 in devDependencies. I realized rollup-plugin-typescript2 was overriding the tslib version. I was using v0.20.1; upgraded to v0.27.1.

Like @ezolenko says: While npm i tslib -D works, this should not be used as the solution, because:

tslib is already a dependency of rollup-plugin-typescript2, how does it end up missing on your system?

Do you use npm install or something else?

What worked in my case and is the better solution is to delete package-lock and node_modules and reinstall.

rm -rf ./node_modules
rm -rf ./package-lock.json
npm i

You can simply upgrade your tslib version. I did mine from tslib@2.0.0 to tslib@^2.2.0 which is the latest one by the time and it fixed my "__spreadArray" issue.

tslib is already a dependency of rollup-plugin-typescript2, how does it end up missing on your system?

Do you use npm install or something else?

If you post your tsconfig, rollup config and package.json, somebody might spot something amiss.

Indeed, with "moduleResolution": "node", it works as expected. Reading https://www.typescriptlang.org/docs/handbook/module-resolution.html, I think "node" is a better fit for me, so I’ll just use it. May I suggest mentioning/recommending it in the README?

I see that you opened #14 for the "classic" case, so I’ll close this. Thanks!