- 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.
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.
Option 1: Import the Setup Module First (Recommended)
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:
- Ensuring TextEncoder and TextDecoder are properly available in the global scope
- Creating a custom PlatformNode implementation that TensorFlow.js will use
- Applying the patch before any TensorFlow.js code is executed
Troubleshooting
If you still encounter issues:
- Make sure the setup module is imported before any other modules that might use TensorFlow.js
- Check your bundler configuration to ensure it's not removing the patch code (it's marked as having side effects)
- Try using the CommonJS approach if you're having issues with ES modules
- 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.