brainy/TENSORFLOW_NODEJS.md
David Snelling 19ed3dd081 **refactor(tests): consolidate and replace outdated environment test scripts**
- Removed obsolete scripts: `test-all-environments.js`, `test-fallback-function.js`, `test-fallback-simple.js`, `test-fix.js`, `test-tensorflow-textencoder.js`, `test-unified-encoding.js`, and `test-worker-utils.js`.
- Introduced `scripts/comprehensive-test.js` as a unified testing script covering all environments: Browser, Node.js, and CLI.
- Added `examples/cli-wrapper-example.js` to demonstrate a proper CLI implementation with TensorFlow.js initialization.

This refactor simplifies the testing structure by consolidating redundant scripts into a single comprehensive script while ensuring robust cross-environment coverage.
2025-07-14 11:12:51 -07:00

2.6 KiB

Using TensorFlow.js with Brainy in Node.js Environments

This document provides guidance on resolving TensorFlow.js compatibility issues when using Brainy in Node.js environments, particularly with ES modules.

Common Issues

When using Brainy with TensorFlow.js in Node.js environments, you might encounter errors like:

TypeError: this.util.TextEncoder is not a constructor

This occurs due to how TensorFlow.js initializes its platform detection in ES modules environments.

Solution

Brainy includes a built-in patch to address these issues. The patch is automatically applied when you import Brainy, but in some complex project setups, you might need to take additional steps.

For the most reliable solution, explicitly import Brainy's setup module before any other imports that might use TensorFlow.js:

// Import the setup module first to apply TensorFlow.js patches
import '@soulcraft/brainy/setup';

// Then import and use Brainy or TensorFlow.js
import { BrainyData } from '@soulcraft/brainy';
// ... your code here

Option 2: Apply the Patch Directly

If you need more control, you can directly apply the patch:

// Import and apply the patch directly
import { applyTensorFlowPatch } from '@soulcraft/brainy/utils/textEncoding';
applyTensorFlowPatch();

// Then import and use TensorFlow.js
import * as tf from '@tensorflow/tfjs';
// ... your code here

Option 3: For CommonJS Environments

If you're using CommonJS modules:

// Apply the patch first
require('@soulcraft/brainy/dist/setup.js');

// Then require TensorFlow.js or Brainy
const brainy = require('@soulcraft/brainy');
// ... your code here

How It Works

The patch works by:

  1. Ensuring TextEncoder and TextDecoder are properly available in the global scope
  2. Creating a custom PlatformNode implementation that TensorFlow.js will use
  3. Applying the patch before any TensorFlow.js code is executed

Troubleshooting

If you still encounter issues:

  1. Make sure the setup module is imported before any other modules that might use TensorFlow.js
  2. Check your bundler configuration to ensure it's not removing the patch code (it's marked as having side effects)
  3. Try using the CommonJS approach if you're having issues with ES modules
  4. If using a bundler like webpack or rollup, ensure it's configured to handle Node.js built-ins properly

Need More Help?

If you continue to experience issues, please open an issue on our GitHub repository with details about your environment and how you're using Brainy.