Skip to content

TypeScript Module Setup

Status: Placeholder - Content coming soon

Prerequisites: Your First Module, Node.js and npm installed

Planned Content

This tutorial teaches you how to set up and work with TypeScript/npm modules in the r2r monorepo.

What You'll Learn

  • Create TypeScript modules in the monorepo
  • Configure package.json and tsconfig.json
  • Register npm modules in .r2r/eac/repository.yml
  • Build TypeScript modules with r2r build
  • Test TypeScript modules with r2r test
  • Manage npm dependencies between modules
  • Integrate with Go modules in the same repo

Tutorial Structure

  1. Understanding npm module support
  2. Module types: npm-app, npm-lib, npm-tool
  3. Build process: TypeScript → JavaScript
  4. Artifact generation
  5. Integration with monorepo

  6. Creating a TypeScript module

  7. Directory structure: go/my-ts-app/
  8. Initialize npm: npm init
  9. Install TypeScript: npm install -D typescript
  10. Configure tsconfig.json

  11. Module contract configuration

  12. Add to .r2r/eac/repository.yml
  13. Define module type (npm-app, npm-lib)
  14. Specify dependencies
  15. Configure build settings

  16. Writing TypeScript code

  17. Create src/ directory
  18. Write TypeScript implementation
  19. Follow TypeScript best practices
  20. Type definitions and interfaces

  21. Testing TypeScript modules

  22. Install test framework (Jest, Vitest)
  23. Write unit tests
  24. Configure test scripts
  25. Run tests: r2r test <module>

  26. Building TypeScript modules

  27. Build command: r2r build <module>
  28. Compiles TypeScript to JavaScript
  29. Generates artifacts
  30. Understand build output

  31. Managing dependencies

  32. Internal dependencies (other modules)
  33. External dependencies (npm packages)
  34. Lock files (package-lock.json)
  35. Dependency validation

  36. Mixed Go and TypeScript monorepo

  37. Go modules and npm modules together
  38. Cross-module dependencies
  39. Build order considerations
  40. Integration testing

Example: TypeScript Service Module

The tutorial creates a TypeScript service:

Directory structure:

go/api-client/
├── src/
│   ├── index.ts
│   ├── client.ts
│   └── types.ts
├── tests/
│   └── client.test.ts
├── package.json
├── tsconfig.json
└── README.md

Module contract (.r2r/eac/repository.yml):

modules:
  - name: api-client
    type: npm-lib
    root: go/api-client
    dependencies: []
    artifacts:
      - dist/index.js
      - dist/index.d.ts

Package.json:

{
  "name": "@r2r/api-client",
  "version": "1.0.0",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "build": "tsc",
    "test": "jest",
    "lint": "eslint src"
  },
  "devDependencies": {
    "typescript": "^5.0.0",
    "jest": "^29.0.0",
    "@types/jest": "^29.0.0"
  }
}

TypeScript config:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist", "tests"]
}

Key Concepts Covered

  • TypeScript module configuration
  • npm module types in monorepo
  • Building and testing TypeScript
  • Module dependencies
  • Mixed language monorepo

Building and Testing

# Register module (done once)
# Edit .r2r/eac/repository.yml

# Install dependencies
cd go/api-client
npm install

# Build module
r2r eac build api-client
# Runs: npm run build

# Test module
r2r eac test api-client
# Runs: npm test

# View artifacts
r2r eac show artifacts api-client

npm Module Types

npm-app: - Standalone application - Produces executable artifact - Example: CLI tool, web server

npm-lib: - Shared library - Consumed by other modules - Example: API client, utilities

npm-tool: - Build/development tool - Used during build process - Example: Code generator, linter

Best Practices

  • Use TypeScript strict mode
  • Write comprehensive type definitions
  • Include .d.ts files in artifacts
  • Version lock dependencies (package-lock.json)
  • Use consistent tsconfig across modules
  • Write unit tests for all code
  • Lint TypeScript code (ESLint)
  • Keep modules focused and small

Testing TypeScript Modules

Jest configuration:

{
  "preset": "ts-jest",
  "testEnvironment": "node",
  "roots": ["<rootDir>/tests"],
  "testMatch": ["**/*.test.ts"]
}

Example test:

import { APIClient } from '../src/client';

describe('APIClient', () => {
  it('should make GET requests', async () => {
    const client = new APIClient('https://api.example.com');
    const result = await client.get('/users');
    expect(result).toBeDefined();
  });
});

Mixed Monorepo Example

Repository with both Go and TypeScript:

go/
├── eac-commands/        # Go CLI
├── eac-web-ui/          # TypeScript React app
└── api-client/          # TypeScript library

Dependencies: - eac-web-ui depends on api-client (TypeScript → TypeScript) - eac-commands is independent (Go)

Build order: 1. Build api-client (TypeScript lib) 2. Build eac-web-ui (depends on api-client) 3. Build eac-commands (independent)

Common Issues

Issue: Module not found - Solution: Run npm install in module directory

Issue: Build fails - Solution: Check tsconfig.json configuration

Issue: Tests not running - Solution: Verify test script in package.json

Issue: Artifact not generated - Solution: Check outDir in tsconfig.json matches artifacts in contract

Integration with CI/CD

# .github/workflows/ci.yml
jobs:
  test-typescript:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Build TypeScript modules
        run: r2r eac build api-client

      - name: Test TypeScript modules
        run: r2r eac test api-client

Next Steps

After completing this tutorial, you can work with TypeScript modules in the monorepo. Explore other specialized topics based on your needs: Effective BDD Scenarios, Architecture Documentation, or Security Scanning.


Tutorials | How-to Guides | Explanation | Reference

You are here: Tutorials — learning-oriented guides that take you through steps to complete a project.