jsii-rosetta
The `jsii-rosetta` library, currently at version 5.9.39, is an essential tool within the `jsii` ecosystem, developed by AWS. Its core purpose is to translate code snippets written in one `jsii`-supported language (primarily TypeScript) into other target languages such as Python, Java, C#, and Go. This functionality is crucial for projects like the AWS Cloud Development Kit (CDK), enabling documentation to display examples across various programming languages from a single source. `jsii-rosetta` maintains a rapid and continuous release cadence, frequently updating its internal `jsii` and TypeScript dependencies to ensure compatibility and leverage the latest `jsii` features. Its key differentiator is its specialized focus on `jsii`-compliant code translation, providing robust tools for syntax analysis, type resolution, and idiomatic translation across multiple language runtimes.
Common errors
-
Error: Cannot find module 'jsii-rosetta' or its corresponding type declarations.
cause The `jsii-rosetta` package is not installed, or there's an issue with module resolution in your build system (e.g., TypeScript configuration).fixRun `npm install jsii-rosetta` or `yarn add jsii-rosetta`. Verify your `tsconfig.json` includes `node_modules` in its `typeRoots` or `moduleResolution` settings. -
TypeError: rosetta.translateTypeScript is not a function
cause This usually indicates that `Rosetta` was not correctly imported or instantiated, or an outdated version of `jsii-rosetta` is being used where the API might differ.fixEnsure you are using `import { Rosetta } from 'jsii-rosetta';` and instantiate it with `new Rosetta()`. Check `jsii-rosetta` documentation for API changes if using an older version. -
The Rosetta kernel failed to initialize. Make sure you have a compatible jsii-runtime installed.
cause The internal `jsii` runtime that `jsii-rosetta` depends on could not be found or is incompatible with the installed `jsii-rosetta` version, often due to Node.js version or `jsii` package mismatches.fixEnsure your Node.js version meets the `jsii-rosetta` requirements (e.g., `>= 20.16.0`). Reinstall `jsii-rosetta` to ensure all its peer dependencies, including the `jsii-runtime`, are correctly linked. -
TypeScript diagnostics: Cannot find name 'cdk' (or similar for other `jsii` types)
cause The `jsii-rosetta` instance cannot resolve types from `jsii`-generated libraries (like `aws-cdk-lib`) that are used in the snippet being translated.fixEnsure that the `jsii` packages referenced in your code snippets are installed in your project's `node_modules` (e.g., `npm install aws-cdk-lib`). If running programmatically, you might need to specify a `projectRoot` in `Rosetta`'s options for proper module resolution.
Warnings
- breaking `jsii-rosetta` is tightly coupled with its internal `jsii` and TypeScript dependencies. Upgrading `jsii` in your project without also updating `jsii-rosetta` can lead to build failures or incorrect translations due to API mismatches.
- gotcha The `engines` field in `package.json` specifies a minimum Node.js version. Using an older Node.js version will result in errors and prevent `jsii-rosetta` from functioning correctly.
- gotcha While `jsii-rosetta` aims for accurate translation, highly complex or non-idiomatic TypeScript code (e.g., extensive use of `any`, advanced generics without clear type boundaries, or intricate conditional types) may result in less accurate or failing translations in target languages.
- gotcha When running `jsii-rosetta` programmatically, ensure all `jsii` dependencies required by the snippets being translated are correctly installed and available in the environment where `jsii-rosetta` executes. Missing dependencies will cause type resolution failures during translation.
Install
-
npm install jsii-rosetta -
yarn add jsii-rosetta -
pnpm add jsii-rosetta
Imports
- Rosetta
const Rosetta = require('jsii-rosetta').Rosetta;import { Rosetta } from 'jsii-rosetta'; - TargetLanguage
import { LANGUAGES } from 'jsii-rosetta';import { TargetLanguage } from 'jsii-rosetta'; - renderTypeScriptSnippet
import { renderTypeScriptSnippet } from 'jsii-rosetta';
Quickstart
import { Rosetta, TargetLanguage } from 'jsii-rosetta';
async function translateCode() {
const rosetta = new Rosetta();
const tsSnippet = `
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
new ec2.Vpc(stack, 'MyVpc', {
maxAzs: 2,
natGateways: 1,
});
app.synth();
`;
// Translate to Python
const pythonTranslation = await rosetta.translateTypeScript(tsSnippet, {
language: TargetLanguage.PYTHON,
trimLeadingEmptyLines: true,
indentation: 4,
});
if (pythonTranslation.translation) {
console.log('Python Code:\n', pythonTranslation.translation.source);
} else if (pythonTranslation.didFail) {
console.error('Python translation failed:', pythonTranslation.failure);
}
// Translate to Java
const javaTranslation = await rosetta.translateTypeScript(tsSnippet, {
language: TargetLanguage.JAVA,
trimLeadingEmptyLines: true,
indentation: 4,
});
if (javaTranslation.translation) {
console.log('\nJava Code:\n', javaTranslation.translation.source);
} else if (javaTranslation.didFail) {
console.error('Java translation failed:', javaTranslation.failure);
}
}
translateCode().catch(console.error);