ezolenko / rollup-plugin-typescript2

Rollup plugin for typescript with compiler errors.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`TS2354 This syntax requires an imported helper but module 'tslib' cannot be found.`

bluetech opened this issue · comments

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.

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 rollup/rollup-plugin-typescript#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.

commented

@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?

commented

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.

commented

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

commented

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