Fix Alpine setup to patch transformers backend directly

Instead of creating a stub onnxruntime-node module, patch the
@xenova/transformers backend file to skip the onnxruntime-node
import entirely and use only onnxruntime-web.

This prevents the "Cannot read properties of undefined" error
when the library tries to use the Node backend in a Node.js
environment but only has the web backend available.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Michael Mainguy 2025-11-23 10:44:02 -06:00
parent 28f5d83d67
commit c3f301b799

View File

@ -1,28 +1,43 @@
#!/bin/sh #!/bin/sh
# Alpine Linux setup script # Alpine Linux setup script
# Removes onnxruntime-node to prevent glibc dependency errors # Patches @xenova/transformers to use onnxruntime-web instead of onnxruntime-node
# The transformers library will fall back to onnxruntime-web (WASM) # This is required for Alpine Linux (musl libc) compatibility
echo "Removing onnxruntime-node for Alpine Linux compatibility..." echo "Configuring Transformers.js for Alpine Linux (WASM backend)..."
# Remove the onnxruntime-node directory # Patch the ONNX backend selector to always use onnxruntime-web
rm -rf node_modules/onnxruntime-node ONNX_BACKEND_FILE="node_modules/@xenova/transformers/src/backends/onnx.js"
# Create a stub module so imports don't fail if [ -f "$ONNX_BACKEND_FILE" ]; then
mkdir -p node_modules/onnxruntime-node # Backup original file
cat > node_modules/onnxruntime-node/package.json << 'EOF' cp "$ONNX_BACKEND_FILE" "$ONNX_BACKEND_FILE.bak"
{
"name": "onnxruntime-node", # Replace the backend selection logic to always use ONNX_WEB
"version": "0.0.0-stub", cat > "$ONNX_BACKEND_FILE" << 'EOF'
"main": "index.js", /**
"type": "module" * @file Handler file for choosing the correct version of ONNX Runtime, based on the environment.
} * PATCHED FOR ALPINE LINUX: Always uses onnxruntime-web (WASM backend)
* @module backends/onnx
*/
// Only import onnxruntime-web for Alpine Linux compatibility
import * as ONNX_WEB from 'onnxruntime-web';
/** @type {import('onnxruntime-web')} The ONNX runtime module. */
export let ONNX;
export const executionProviders = [
'wasm'
];
// Always use ONNX_WEB (WASM backend) on Alpine Linux
ONNX = ONNX_WEB.default ?? ONNX_WEB;
EOF EOF
cat > node_modules/onnxruntime-node/index.js << 'EOF' echo "✓ Patched @xenova/transformers backend to use WASM"
// Stub module for Alpine Linux else
// @xenova/transformers will use onnxruntime-web instead echo "✗ Could not find $ONNX_BACKEND_FILE"
export default {}; exit 1
EOF fi
echo "✓ Alpine Linux setup complete - WASM backend will be used" echo "✓ Alpine Linux setup complete - WASM backend will be used"