Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module Compatibility Issue with Node.js and "type": "module" #202

Open
darielnoel opened this issue Jan 24, 2025 · 2 comments
Open

Module Compatibility Issue with Node.js and "type": "module" #202

darielnoel opened this issue Jan 24, 2025 · 2 comments
Labels
bug Something isn't working dx Developer experience feedback discord

Comments

@darielnoel
Copy link
Contributor

Describe the Issue
When using KaibanJS in a Node.js project with "type": "module" specified in package.json, attempting to use import statements to load the library results in an error indicating a missing react dependency. Even after installing react, the error persists. However, switching to require resolves the issue.

Steps to Reproduce

  1. Create a Node.js project with "type": "module" in the package.json file.
  2. Install kaibanjs and other necessary dependencies.
  3. Use import statements to include KaibanJS modules.
  4. Observe the error indicating that react is missing.
  5. Switch to require instead of import, and notice the issue is resolved.

Current vs. Expected Behavior

  • Current Behavior: Using import in a Node.js environment with "type": "module" triggers an error related to react.
  • Expected Behavior: The library should work seamlessly with import syntax without requiring react or other unnecessary dependencies in a Node.js environment.

Environment

  • Node.js Version: [e.g., 18.x]
  • KaibanJS Version: [e.g., 0.1.0]
  • JavaScript Runtime: [Node.js with ESM support]

Possible Cause
The issue might stem from the assumption that ES6 module compilation is intended for browser environments only, where react is a required dependency. This could have led to unintended behavior when using the library in Node.js environments.

@darielnoel darielnoel added bug Something isn't working feedback discord dx Developer experience labels Jan 24, 2025
@darielnoel
Copy link
Contributor Author

darielnoel commented Jan 30, 2025

I've been investigating this issue and created a reproduction example to validate the behavior. Could someone help verify if my findings are correct?

Test Environment:

Test Setup Created:

  • Added example in playground/nodejs-esm/ to test both ESM and CommonJS usage
  • Example includes both formats:
    • ESM version (index.js with import statements)
    • CommonJS version (index.cjs with require statements)

Current Observations:

  1. Both ESM and CommonJS versions work without React dependency issues in Node.js 21
  2. Looking at the build configuration (rollup.config.mjs), React is marked as external for ESM builds:
    const external = isESM
      ? ['react', 'react-dom', 'uuid', 'pino', 'pino-pretty']
      : ['uuid', 'pino', 'pino-pretty'];
  3. The Node.js example appears to work because it only uses zustand's core functionality, not its React-specific features

Questions for Validation:

  1. Is this behavior intended? Should Node.js usage work without React as a dependency?
  2. Does this behavior vary across different Node.js versions? (Tested on Node.js 21)
  3. Should we document any Node.js version requirements or recommendations?
  4. Should the example in playground/nodejs-esm/ be kept as a reference?

The example implements a complete team setup with multiple agents and tasks. Happy to provide more details or modify the test case if needed.

@MartinKunft
Copy link

MartinKunft commented Feb 5, 2025

Thank you for looking into this, the issue is indeed with the version of node, I am using v18.20.5, but testing it with v20.10.0 both .cjs and .js versions work where with node v18.20.5 only the .cjs version works and the .js version throws the react error:
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'react' imported from /Users/martinkunft/dev/ai/testing/KaibanJS/playground/nodejs-esm/node_modules/kaibanjs/dist/bundle.mjs
at packageResolve (node:internal/modules/esm/resolve:844:9)
at moduleResolve (node:internal/modules/esm/resolve:901:20)
at defaultResolve (node:internal/modules/esm/resolve:1121:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
at ModuleWrap. (node:internal/modules/esm/module_job:85:39)
at link (node:internal/modules/esm/module_job:84:36) {
code: 'ERR_MODULE_NOT_FOUND'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dx Developer experience feedback discord
Projects
None yet
Development

No branches or pull requests

2 participants