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

Created on 18 Apr 2017  ·  19Comments  ·  Source: ezolenko/rollup-plugin-typescript2

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
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

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.

Most helpful comment

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

All 19 comments

This is definitely supposed to work transparently (and works for me in my own projects). I'll try your exact setup, thanks.

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.

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!

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"

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

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

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

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?

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 [email protected]: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.

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.

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

npm i tslib -D

worked for me too

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

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

Thank you, It's so helpful

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

npm i [email protected]

It turns out rollup was using [email protected] 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.

Weird behavior, But restarting VS code worked for me. I didn't have the issue previously, it just came up after doing an package install.

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

Thanks. Just added to peerDependencies and it works well for me, do not need to install

I had this error:

semantic error TS2343: This syntax requires an impor
ted helper named '__spreadArray' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alireza-salemian picture alireza-salemian  ·  4Comments

PavaniVaka picture PavaniVaka  ·  12Comments

eddow picture eddow  ·  14Comments

freeman picture freeman  ·  6Comments

JWMB picture JWMB  ·  8Comments