Rollup stops with Unexpected token when it reaches a TypeScript file
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)Short answer
Rollup parses plain JavaScript only. A .ts file needs @rollup/plugin-typescript in the plugins array, with typescript and tslib installed next to it. If you keep JSX in the output with jsx set to preserve, Rollup also needs acorn-jsx to parse it.
Fix it fast
1Find out which cause you have
Open rollup.config.js and check whether a TypeScript plugin is in the plugins array.
grep -n "plugin-typescript\|plugins" rollup.config.*2Match what you see, then jump to its fix
- 1
The error names a .ts file and the plugins array is empty or has no typescript()The TypeScript plugin is not installed or not usedFix - 2
The plugin is configured but the build still fails, and npm ls shows missing peerstypescript or tslib is missingFix - 3
The unexpected token is a < in a .tsx file and tsconfig sets jsx to preserveJSX is preserved and Rollup cannot parse itFix
- 1
What does the error look like?
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
Cause 1 of 3
The TypeScript plugin is not installed or not used
1Check
Rollup does not compile TypeScript on its own. The package list shows whether the plugin is there.
npm ls @rollup/plugin-typescript typescript tslib2Fix
Install the plugin and add it to the plugins array.
npm install --save-dev @rollup/plugin-typescript
// rollup.config.js
import typescript from '@rollup/plugin-typescript';
export default {
input: 'src/index.ts',
output: { dir: 'output', format: 'cjs' },
plugins: [typescript()]
};3Verify
Run the build.
npx rollup -cConfirmed by github.com
Cause 2 of 3
typescript or tslib is missing
1Check
typescript and tslib are peer dependencies of the plugin, so you must install both yourself.
npm ls typescript tslib2Fix
Install them as dev dependencies.
npm install --save-dev typescript tslib3Verify
Run the build again.
npx rollup -cConfirmed by github.com
Cause 3 of 3
JSX is preserved and Rollup cannot parse it
1Check
With jsx set to preserve, the plugin output still contains JSX, which Rollup's own parser cannot read.
grep -n '"jsx"' tsconfig.json2Fix
Add the acorn-jsx plugin through acornInjectPlugins, or let TypeScript compile JSX away (jsx set to react-jsx).
import jsx from 'acorn-jsx';
import typescript from '@rollup/plugin-typescript';
export default {
acornInjectPlugins: [jsx()],
plugins: [typescript({ compilerOptions: { jsx: 'preserve' } })]
};3Verify
Run the build.
npx rollup -cConfirmed by github.com
How do I stop it coming back?
- Add the plugin, typescript and tslib in one commit when a project first uses TypeScript with Rollup.
- Keep the input file extension in rollup.config.js in step with the source files.
- Build in CI from a clean install so a missing peer dependency shows up early.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
SonarLint in VS Code cannot analyze JavaScript or TypeScript files
JS and TS files get no SonarLint findings, while other languages work
JavaScript and TypeScript analysis needs a supported Node.js: 22.12.0 or newer on the 22 line, or 24.0.0 or newer on the 24 line.
2 causes, each with a fixBack end
TypeScript cannot find '@angular/platform-browser' in an Angular project
Cannot find module '@angular/platform-browser' or its corresponding type declarations.
TS2307 means TypeScript could not turn the import into a file.
3 causes, each with a fixBack end
Angular build fails with TS2707 or "has no exported member" after installing a library
Module '"@angular/core"' has no exported member
These errors mean a library was compiled by a newer Angular than your app uses.
3 causes, each with a fixBack end
cdk synth says --app is required
--app is required either in command-line, in cdk.json or in ~/.cdk.json
The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.
3 causes, each with a fixBack end
The AWS SDK in Node.js cannot find any credentials
Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
The AWS SDK walks a fixed credential chain: code, environment variables, the shared files in ~/.aws, then role sources such as ECS or EC2.
3 causes, each with a fixBack end
AWS SDK for JavaScript v3 cannot find credentials, even for DynamoDB Local
A DynamoDB client pointed at http://localhost:8000 fails with the same message
The SDK walked its default credential provider chain and found nothing.
3 causes, each with a fix