Registry / web-framework / python2ts

python2ts

JSON →
library1.4.3jsnpmunverified

python2ts is an AST-based Python-to-TypeScript transpiler that converts Python code into clean, idiomatic TypeScript while preserving type annotations and semantics. The current stable version is 1.4.3 (January 2026), with active development and frequent releases (multiple per week). It supports a wide range of Python features including dataclasses, list/dict comprehensions, pattern matching, f-strings, async/await, decorators, context managers, generators, and standard library modules via the accompanying pythonlib runtime. Unlike other transpilers, python2ts produces readable, maintainable TypeScript output with full type preservation and has minimal runtime overhead, making it suitable for migrating large Python codebases to TypeScript. Requires Node >=22.0.0.

npm install python2ts
INSTALL
IMPORT
SIG · PYTHON2TS
P
python2ts
web-frameworkjavascriptv1.4.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import python2ts from 'python2ts'
const python2ts = require('python2ts')
python2ts is ESM-only starting from v1.0; CommonJS require will throw an error.
transpile
import { transpile } from 'python2ts'
import transpile from 'python2ts'
transpile is a named export, not a default export.
TranspileOptions
import type { TranspileOptions } from 'python2ts/types'
import { TranspileOptions } from 'python2ts'
Type imports should be from the 'types' subpath to avoid runtime overhead.
CLI
import { cli } from 'python2ts'
import * as python2ts from 'python2ts'; python2ts.cli()
CLI is exported as a named function; namespace import works but direct named import is preferred.

Shows basic transpilation of a Python function with type hints and f-string to TypeScript.

import { transpile } from 'python2ts'; const pythonCode = ` def greet(name: str) -> str: return f"Hello, {name}!" `; try { const result = transpile(pythonCode, { filename: 'example.py', runtime: 'pythonlib', // default runtime path }); console.log(result.code); // Output: // import { greet } from "pythonlib/builtins" // function greet(name: string): string { // return `Hello, ${name}!`; // } } catch (error) { console.error('Transpilation failed:', error.message); }
python2ts --version
Debug
Known issues
gotchaThe TranspileOptions type is not exported directly from 'python2ts'; import from 'python2ts/types' instead.
fix
import type { TranspileOptions } from 'python2ts/types';
affects: >=1.0
deprecatedUsing '--target es5' in CLI options is deprecated; ES2015+ is required.
fix
Remove --target flag or use a modern target like es2020.
affects: >=1.4
gotchaTranspiled code depends on the 'pythonlib' package at runtime; it is not a standalone output.
fix
Ensure 'pythonlib' is installed in your project: npm install pythonlib
affects: >=1.0
breakingIn pythonlib v2.0.0, hashlib functions digest(), hexdigest(), pbkdf2Hmac(), scrypt(), and compareDigest() now return Promises and must be awaited.
fix
Update transpiled code to await these calls, e.g., const hash = await digest(data);
affects: >=2.0.0 (pythonlib)
gotchaCLI output defaults to stdout; using -o with an existing file will overwrite without confirmation.
fix
Ensure the output file path is correct or that you have a backup.
affects: >=1.0
breakingVersion 1.0 dropped CommonJS support; the package is ESM-only.
fix
Use ES module imports (import/export) in your project. Update tsconfig to include 'module': 'ESNext'.
affects: >=1.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/python2ts/index.js not supported.
Python2ts is ESM-only; using require() in a CommonJS context fails.
fix
Switch your project to ES modules or use dynamic import(): const python2ts = await import('python2ts');
TypeError: python2ts.transpile is not a function
Incorrect import: likely using default import instead of named import.
fix
Use import { transpile } from 'python2ts';
Cannot find module 'pythonlib/builtins' or similar import in transpiled output.
The 'pythonlib' runtime package is not installed.
fix
Run npm install pythonlib in the target project.
SyntaxError: Unexpected token 'export' at ...
The transpiled output uses ES module syntax but is loaded in a CommonJS context.
fix
Ensure the transpiled file is loaded as an ES module (e.g., use .mjs extension or { 'type': 'module' } in package.json).
Upgrade
Version history
1.4.3latest on npm
Audit
Dependencies
pythonlibrequiredProvides runtime implementations for Python standard library modules (e.g., collections, itertools, hashlib, os) used in transpiled code.
Agent activity
4 hits · last 30 days
node
4
Resources
python2ts — npm install python2ts · libregistry