{"id":16809,"library":"ern-api-gen","title":"Electrode Native API Generator","description":"ern-api-gen is a foundational component of the Electrode Native ecosystem, designed to automate the creation of React Native API projects. It processes a Swagger (OpenAPI 2.x) schema to generate client-side API code for JavaScript (React Native), Android (Java), and iOS (Swift). This generated code leverages `react-native-electrode-bridge` to enable robust bi-directional message exchange between the native and JavaScript layers. While built upon the principles of Swagger Codegen 2.x, `ern-api-gen` introduces custom generators like `ErnAndroidApiCodegen` and `ErnES6ApiCodegen`, specifically adapted for Electrode Native's architecture and differing significantly in ES6 generation compared to upstream Swagger. Currently at version 0.54.0, the package maintains an active development pace, frequently releasing updates to ensure compatibility with evolving React Native versions (e.g., 0.81) and addressing various build and dependency-related issues. Its primary role is as a command-line interface (CLI) tool, deeply integrated into the Electrode Native CLI for streamlined API project scaffolding within a monorepo-like development workflow.","status":"active","version":"0.54.0","language":"javascript","source_language":"en","source_url":"https://github.com/electrode-io/electrode-native","tags":["javascript","android","api","electrode","ern","ios","node","react-native","typescript"],"install":[{"cmd":"npm install ern-api-gen","lang":"bash","label":"npm"},{"cmd":"yarn add ern-api-gen","lang":"bash","label":"yarn"},{"cmd":"pnpm add ern-api-gen","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This library enables bi-directional communication in the generated APIs, serving as a key runtime dependency for the code ern-api-gen produces, rather than a direct dependency of ern-api-gen itself.","package":"react-native-electrode-bridge","optional":false},{"reason":"Used internally by the Electrode Native CLI for parsing command-line arguments to `ern-api-gen` and other Electrode Native commands.","package":"yargs","optional":false},{"reason":"Involved in resolving module paths, often for internal asset loading or build processes, as indicated by recent changelog updates.","package":"path-loader","optional":false},{"reason":"A transitive dependency that previously caused build failures, highlighting its presence in the package's dependency tree, now patched.","package":"cuid2","optional":false}],"imports":[{"note":"This package is primarily a CLI tool. Direct programmatic imports are typically only for advanced scenarios like extending or customizing the generator itself. `DefaultCodegen` is a base class for the internal code generators, adapted from Swagger, not a common API for end-users.","symbol":"DefaultCodegen","correct":"import { DefaultCodegen } from 'ern-api-gen/dist/src/DefaultCodegen'"},{"note":"Another example of an internal class for those looking to extend or customize the ES6 API generation logic. This is not for consuming generated APIs, but for modifying the generator's behavior.","symbol":"ErnES6ApiCodegen","correct":"import { ErnES6ApiCodegen } from 'ern-api-gen/dist/src/languages/ErnES6ApiCodegen'"},{"note":"Similar to `ErnES6ApiCodegen`, this represents an internal class for advanced customization of the Android API generation. It is not intended for standard application development, where the generated API client would be imported from its own package.","symbol":"ErnAndroidApiCodegen","correct":"import { ErnAndroidApiCodegen } from 'ern-api-gen/dist/src/languages/ErnAndroidApiCodegen'"}],"quickstart":{"code":"/* \n  The primary way to use ern-api-gen is via the Electrode Native CLI.\n  First, ensure you have the Electrode Native CLI installed globally:\n  npm install -g ern-cli\n\n  Then, in your terminal, use the `ern create-api` command.\n  Replace `path/to/your/swagger.json` with your actual OpenAPI/Swagger schema file path.\n  Replace `MyAwesomeApi` with your desired API name.\n*/\n\nconst SCHEMA_FILE_PATH = 'path/to/your/swagger.json'; // e.g., './api-spec/my-service.json'\nconst API_NAME = 'MyAwesomeApi'; // This will be the name of your generated API package\n\nconsole.log(`\nTo generate a new Electrode Native API project named '${API_NAME}'\nfrom your Swagger/OpenAPI schema located at '${SCHEMA_FILE_PATH}',\nrun the following command in your terminal:\n`);\n\nconsole.log(`ern create-api ${API_NAME} --schemaPath ${SCHEMA_FILE_PATH}`);\n\nconsole.log(`\nThis will create a new directory named '${API_NAME}' containing the generated\nJavaScript (React Native), Android, and iOS client code, along with models.\n`);\n\nconsole.log(`\nAfter generation, you can publish the created API project to npm.\nFor more details, refer to the official Electrode Native documentation:\nhttps://native.electrode.io/cli-commands/create-api\n`);\n","lang":"typescript","description":"Demonstrates the canonical command-line interface usage of `ern-api-gen` via the Electrode Native CLI to generate a new API project from a Swagger/OpenAPI schema."},"warnings":[{"fix":"Update all package references and usage in `package.json` and code from `ern-message-gen` to `ern-api-gen`.","message":"The package underwent a significant rename from `ern-message-gen` to `ern-api-gen` on August 9, 2017. Any existing projects or scripts referencing the older package name will require updates to function correctly with newer versions.","severity":"breaking","affected_versions":"<=0.x (pre-August 2017)"},{"fix":"Avoid direct reliance on undocumented or internal APIs of `ern-api-gen`. When upgrading, always consult the official Electrode Native documentation and changelogs for any breaking changes related to its CLI tools.","message":"Early versions of the generator saw major internal architectural shifts, including a conversion from Java (initial codebase) to JavaScript (Apr 2017), and subsequently a full conversion to TypeScript (Apr-May 2018/2019). These changes mean that internal APIs are highly unstable across major historical versions, making direct programmatic integration with internal components unreliable.","severity":"breaking","affected_versions":"<=0.x (pre-TypeScript conversion)"},{"fix":"Regularly update `ern-api-gen` to the latest stable version compatible with your Electrode Native and React Native ecosystem. Refer to Electrode Native's official release notes and compatibility matrices.","message":"Ensuring compatibility with your specific React Native and Android Gradle Plugin (AGP) versions is crucial. `ern-api-gen` is frequently updated to support newer React Native versions (e.g., 0.77, 0.81) and AGP versions (e.g., AGP 8). Using an `ern-api-gen` version that predates support for your project's React Native or AGP version can lead to build failures and unexpected runtime issues.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade `ern-api-gen` to the latest patch release (e.g., `0.53.7` or newer) which includes fixes for known dependency issues. Consider cleaning your `node_modules` and regenerating `package-lock.json` or `yarn.lock` files if issues persist.","message":"Specific dependency resolution problems, such as 'cuid2 build error' or issues related to `path-loader` resolutions, have been identified and patched in certain recent versions. These often manifest as cryptic build errors due to conflicts in transitive dependencies or incorrect module resolution.","severity":"gotcha","affected_versions":"Certain 0.53.x patch versions"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Update `ern-api-gen` to version `0.53.7` or higher, which contains a fix for this specific build error. Ensure your `yarn.lock` or `package-lock.json` is updated and dependencies are reinstalled.","cause":"A transitive dependency `cuid2` encountered build-time issues, potentially due to incompatible tooling or environment configurations.","error":"cuid2 build error"},{"fix":"Ensure `ern-api-gen` is updated to a version that addresses Metro configuration for `.mjs` (e.g., `0.51.2` or newer). Verify your project's Metro configuration supports `.mjs` files.","cause":"Metro bundler, used by React Native, might fail to resolve modules with `.mjs` extensions or encounter other ESM/CommonJS interoperability issues.","error":"Error: Cannot find module '@babel/runtime/helpers/esm/defineProperty' or similar ESM/CJS resolution errors."},{"fix":"Upgrade `ern-api-gen` to a version that officially supports your current Android Gradle Plugin (AGP) version (e.g., `0.53.2` for AGP 8). Confirm your Electrode Native setup and Gradle configuration are consistent with recommended versions.","cause":"This error typically indicates that the Electrode Native CLI, or `ern-api-gen`'s underlying Android generation, cannot properly locate or configure the Android project, often due to an incompatible Android Gradle Plugin (AGP) version.","error":"Error: Gradle project 'app' not found"}],"ecosystem":"npm","meta_description":null}