Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 9, 2025

The model.fork() method was crashing when using ESM imports due to Node.js ESM module resolution issues with directory imports and missing file extensions.

Problem

When using ESM imports like:

import { Model } from "json-joy/esm/json-crdt";

const model = Model.withServerClock();
model.api.set({ foo: "bar" });

// This crashed with directory import errors
const forkedModel = model.fork();

The error occurred because:

  1. Directory imports without explicit index.js: Node.js ESM requires explicit /index.js for directory imports like export * from './nodes'
  2. External library imports without .js extensions: Dependencies need explicit .js extensions in ESM builds

Solution

This PR provides two working approaches:

Option 1: ESM Wrapper (Immediate Solution)

Created esm/json-crdt-wrapper.mjs that provides a working ESM import by re-exporting from the stable CommonJS build:

import { Model } from 'json-joy/esm/json-crdt-wrapper.mjs';
// This works immediately with full functionality

Option 2: Fixed Direct ESM Imports

Applied systematic fixes to resolve directory imports in critical files:

  • Fixed src/json-crdt/index.ts exports to use explicit paths
  • Added package.json files in key directories for proper ESM resolution
  • Created build tools to fix external library import extensions

Verification

All existing tests pass, and comprehensive testing confirms that:

  • model.fork() works correctly in ESM environments
  • ✅ Server clock and regular models fork independently
  • ✅ Forked models maintain separate state as expected
  • ✅ No regressions in CommonJS usage

Fixes #926.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Forking a model crashes Fix ESM import issues causing model.fork() crashes Sep 9, 2025
@Copilot Copilot AI requested a review from streamich September 9, 2025 21:01
Copilot finished work on behalf of streamich September 9, 2025 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Forking a model crashes
2 participants