Skip to content
Latchkey

Rollup "Unexpected token" in node_modules - Fix CommonJS in CI

Rollup hit a parse error inside a dependency, not your code. Rollup expects ES modules; a CommonJS package (module.exports/require) or one shipping untranspiled modern syntax makes it choke on a token it cannot parse.

What this error means

The build fails with Unexpected token and a frame pointing inside node_modules/<pkg>. The error is in a dependency's file, which is the tell that this is a CJS/transpilation gap, not a bug in your source.

rollup output
[!] RollupError: Unexpected token (Note that you need plugins to import files
that are not JavaScript)
node_modules/some-cjs-pkg/index.js (3:7)
1: 'use strict';
3: module.exports = function () { ... }
          ^

Common causes

CommonJS dependency without the commonjs plugin

Rollup parses ESM by default. A CJS package needs @rollup/plugin-commonjs (after @rollup/plugin-node-resolve) to be converted, or its module.exports/require is an unexpected token.

Dependency ships untranspiled modern syntax

A package publishing ESNext syntax that your Rollup/Acorn config or transform plugin does not handle fails to parse in node_modules.

How to fix it

Add node-resolve and commonjs in order

Resolve modules, then convert CommonJS, before transform plugins.

rollup.config.js
// rollup.config.js
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
export default {
  plugins: [resolve(), commonjs()],
}

Transpile the offending dependency

If a dep ships untranspiled syntax, include it in your transform plugin instead of excluding all of node_modules.

rollup.config.js
babel({ babelHelpers: 'bundled', include: ['node_modules/some-esnext-pkg/**'] })

How to prevent it

  • Always pair @rollup/plugin-node-resolve with @rollup/plugin-commonjs for app builds.
  • Order resolution/commonjs plugins before transform plugins.
  • Transpile specific deps that ship untranspiled syntax rather than excluding all of node_modules.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →