|
| 1 | +# OpenCode Node.js Migration |
| 2 | + |
| 3 | +This directory contains the Node.js-compatible version of OpenCode, migrated from Bun. |
| 4 | + |
| 5 | +## Running OpenCode with Node.js |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- Node.js v22+ (tested with v25.2.1) |
| 10 | +- pnpm (for package management) |
| 11 | + |
| 12 | +### Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +cd /Users/matt/repo/opencode |
| 16 | +pnpm install |
| 17 | +``` |
| 18 | + |
| 19 | +### Running |
| 20 | + |
| 21 | +**Unix/Linux/macOS:** |
| 22 | +```bash |
| 23 | +cd packages/opencode |
| 24 | +./opencode-node.mjs [arguments] |
| 25 | +``` |
| 26 | + |
| 27 | +**Windows (PowerShell):** |
| 28 | +```powershell |
| 29 | +cd packages\opencode |
| 30 | +.\opencode-node.ps1 [arguments] |
| 31 | +``` |
| 32 | + |
| 33 | +**Windows (Command Prompt):** |
| 34 | +```cmd |
| 35 | +cd packages\opencode |
| 36 | +opencode-node.bat [arguments] |
| 37 | +``` |
| 38 | + |
| 39 | +**Direct Node.js command (all platforms):** |
| 40 | +```bash |
| 41 | +node --loader ./loader.mjs --conditions=browser --import ./global-bun-shim.mjs --import tsx/esm ./src/index.ts [arguments] |
| 42 | +``` |
| 43 | + |
| 44 | +### Examples |
| 45 | + |
| 46 | +```bash |
| 47 | +# Show help |
| 48 | +./opencode-node.mjs --help |
| 49 | + |
| 50 | +# Show version |
| 51 | +./opencode-node.mjs --version |
| 52 | + |
| 53 | +# Run OpenCode |
| 54 | +./opencode-node.mjs |
| 55 | +``` |
| 56 | + |
| 57 | +## Migration Details |
| 58 | + |
| 59 | +### Key Changes |
| 60 | + |
| 61 | +1. **Bun Compatibility Layer** (`src/util/bun-compat.ts`) |
| 62 | + - Implements Bun APIs using Node.js equivalents |
| 63 | + - Provides: `Bun.$`, `Bun.file`, `Bun.write`, `Bun.spawn`, `Bun.Glob`, `Bun.which`, `Bun.stderr`, `Bun.color` |
| 64 | + |
| 65 | +2. **Custom ESM Loader** (`loader.mjs`) |
| 66 | + - Handles asset file imports (.txt, .scm, .md, .wasm, etc.) |
| 67 | + - Resolves `bun:` protocol imports to compatibility layers |
| 68 | + - Prevents tsx from transforming TypeScript files in node_modules to CJS |
| 69 | + |
| 70 | +3. **Global Bun Shim** (`global-bun-shim.mjs`) |
| 71 | + - Injects Bun compatibility layer into global scope |
| 72 | + - Allows third-party libraries expecting `Bun` to work |
| 73 | + |
| 74 | +4. **Text File Import Conversion** |
| 75 | + - Converted all `.txt` file imports to use `fs.readFileSync()` |
| 76 | + - Added necessary imports (`fs`, `path`, `fileURLToPath`) |
| 77 | + |
| 78 | +5. **Package Updates** |
| 79 | + - Changed package manager from `bun` to `pnpm` |
| 80 | + - Replaced `bun-pty` with `node-pty` |
| 81 | + - Updated tsconfig to use `@tsconfig/node22` |
| 82 | + - Added path mapping for `bun` → `./src/util/bun-compat.ts` |
| 83 | + |
| 84 | +### Known Limitations |
| 85 | + |
| 86 | +1. **bun:ffi Stub**: The `bun:ffi` module is stubbed out, so native FFI functionality from `@opentui/core` may be limited |
| 87 | +2. **Experimental Loader Warning**: Node.js shows a warning about the experimental loader API (can be suppressed with `NODE_NO_WARNINGS=1`) |
| 88 | +3. **Performance**: May be slightly slower than Bun due to different runtime optimizations |
| 89 | + |
| 90 | +### Files Modified |
| 91 | + |
| 92 | +**Core Compatibility:** |
| 93 | +- `src/util/bun-compat.ts` - Bun API compatibility layer |
| 94 | +- `src/util/bun-ffi-compat.ts` - Bun FFI stub |
| 95 | +- `loader.mjs` - Custom ESM loader |
| 96 | +- `global-bun-shim.mjs` - Global Bun injection |
| 97 | + |
| 98 | +**Import Fixes:** |
| 99 | +- `src/provider/models-macro.ts` - Removed macro, added Bun compat import |
| 100 | +- `src/provider/models.ts` - Removed macro import, added Bun compat |
| 101 | +- `src/util/filesystem.ts` - Added Bun import, fixed `fs.exists` |
| 102 | +- `src/util/log.ts` - Added Bun import |
| 103 | +- `src/file/ignore.ts` - Added Bun import |
| 104 | +- `src/file/ripgrep.ts` - Added Bun import |
| 105 | +- `src/cli/ui.ts` - Added Bun import |
| 106 | +- `src/session/prompt.ts` - Fixed duplicate fs imports |
| 107 | +- 20+ files - Converted .txt imports to fs.readFileSync |
| 108 | + |
| 109 | +**Configuration:** |
| 110 | +- `package.json` - Updated package manager and dependencies |
| 111 | +- `tsconfig.json` - Added bun path mapping |
| 112 | +- `pnpm-workspace.yaml` - Created for pnpm |
| 113 | +- `packages/sdk/js/package.json` - Added src to files array |
| 114 | + |
| 115 | +**Launchers:** |
| 116 | +- `opencode-node.mjs` - Unix/macOS launcher |
| 117 | +- `opencode-node.bat` - Windows batch launcher |
| 118 | +- `opencode-node.ps1` - Windows PowerShell launcher |
| 119 | + |
| 120 | +## Testing |
| 121 | + |
| 122 | +Tested on: |
| 123 | +- ✅ macOS (darwin-arm64) with Node.js v25.2.1 |
| 124 | +- ⏳ Windows Server 2022 (pending) |
| 125 | + |
| 126 | +## Future Work |
| 127 | + |
| 128 | +- Package as standalone executable using `pkg`, `nexe`, or `caxa` |
| 129 | +- Implement full `bun:ffi` support using node-ffi-napi |
| 130 | +- Add CI/CD for automated testing |
| 131 | +- Create installers for Windows, macOS, Linux |
0 commit comments