Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Rollup stops with Unexpected token when it reaches a TypeScript file

Error[!] 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.

  • Applies to Rollup 4, @rollup/plugin-typescript, TypeScript 5.9, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.*
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The plugin is configured but the build still fails, and npm ls shows missing peerstypescript or tslib is missingFix
    3. 3The unexpected token is a < in a .tsx file and tsconfig sets jsx to preserveJSX is preserved and Rollup cannot parse itFix

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 tslib

2Fix

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

Confirmed by github.com

Did this fix it?

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 tslib

2Fix

Install them as dev dependencies.

npm install --save-dev typescript tslib

3Verify

Run the build again.

npx rollup -c

Confirmed by github.com

Did this fix it?

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

2Fix

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

Confirmed by github.com

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.