Sucrase

raw JSON →
3.35.1 verified Sat Apr 25 auth: no javascript

Sucrase v3.35.1 is a super-fast JavaScript/TypeScript transpiler focused on modern JS runtimes. Unlike Babel or TypeScript, it only compiles non-standard language extensions (JSX, TypeScript, Flow) and ES modules to CommonJS, offering ~20x speed improvement. It ships TypeScript types, requires Node >=16 or 14.17+, and is actively maintained on GitHub. Primarily used for development builds where type checking is handled separately.

error Cannot find module 'sucrase/register'
cause Import path typo or incorrect module resolution.
fix
Use require('sucrase/register') or check that sucrase is installed as a devDependency.
error Unsupported transform: decorators
cause Sucrase does not support decorators (neither legacy nor TC39).
fix
Preprocess decorators with a separate tool like Babel or use a TypeScript transformer.
error TypeScript error: Cannot use namespace 'Foo' as a type.
cause Sucrase removes types but does not validate TypeScript; you still need `tsc` for type checking.
fix
Run tsc separately or use the --noEmit flag for type checking before Sucrase.
deprecated Using `sucrase/register` for dynamic require hooks may be deprecated in favor of official Node.js loader hooks.
fix Consider using Node.js --loader or --experimental-loader flags with a custom hook.
gotcha TypeScript enums: Sucrase compiles enums but does not support `const enum`s that rely on cross-file inlining.
fix Enable TypeScript's `isolatedModules` flag to disallow `const enum`s.
breaking v3 dropped support for Node.js versions <14.17 and removed some legacy options like `sourceMap` and `sourceMapTarget`.
fix Use `sourceMaps` and `sourceMapUrl` options instead. Upgrade to Node >=14.17.
gotcha JSX transform: Default runtime is `classic` (React.createElement). Some users encounter 'React is not defined' errors if not importing React.
fix Use `jsxRuntime: 'automatic'` and add `jsxImportSource` or ensure React is globally available.
npm install sucrase
yarn add sucrase
pnpm add sucrase

Read a TypeScript file, remove type annotations, and convert ES modules to CommonJS.

import { transform } from 'sucrase';
import * as fs from 'fs';

const code = fs.readFileSync('input.ts', 'utf8');
const result = transform(code, {
  transforms: ['typescript', 'imports'],
  filePath: 'input.ts',
});
console.log(result.code);