You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Watson Classifier API Node.js示例报错:source.on is not a function 求助

Fixing "source.on is not a function" Error with Watson Classifier Create API in Node.js

Looks like you're hitting a common snag when working with readable streams in the Watson SDK. Let's break down what's causing this and walk through how to fix it step by step.

What's the Root Cause?

The error source.on is not a function almost always means the Watson SDK expects a valid readable stream for the training_data parameter, but it's receiving something else—like undefined, a non-stream object, or a broken stream that failed to initialize.

Step-by-Step Fixes

1. Verify Your Training File Path

First, double-check that the path stored in config.data.prepared.training is correct. If the file doesn't exist or the path is misconfigured, fs.createReadStream() might return an invalid stream (or throw an error that's being swallowed elsewhere in your code).

Add a quick debug log to confirm the path:

console.log('Training file path:', config.data.prepared.training);

Then test the stream directly to catch file-related errors:

const trainingStream = fs.createReadStream(config.data.prepared.training);
trainingStream.on('error', (err) => {
  console.error('Failed to create training stream:', err);
});

If you see an error like ENOENT: no such file or directory, fix the path to point to your valid CSV training file.

2. Check for Typos in Parameter Names

Looking at your code snippet, line 67 shows params.trainin... which looks like a truncated line. Make sure you didn't misspell training_data in your actual code—if the parameter name is wrong, the SDK won't receive the stream it expects, leading to this error.

Confirm your params object correctly defines:

training_data: fs.createReadStream(config.data.prepared.training)

3. Update Your Watson SDK

Older versions of the watson-developer-cloud package might have inconsistent handling for stream parameters. Update to the latest stable version to rule out SDK-specific bugs:

npm update watson-developer-cloud

4. Try Using a Buffer Instead of a Stream (Troubleshooting Step)

If streams are causing persistent issues, temporarily switch to reading the file as a Buffer to confirm the rest of your code works:

const fs = require('fs');

// Replace the stream with a file buffer
const trainingBuffer = fs.readFileSync(config.data.prepared.training);

const params = {
  metadata: {
    language: 'en',
    name: '*********************'
  },
  training_data: trainingBuffer
};

If this works, the problem is specifically with how your stream is being created or passed to the SDK.

5. Validate Your Training File Format

While this isn't the direct cause of the source.on error, make sure your training CSV follows Watson's requirements:

  • First row must have two columns: class and text (or your custom labels)
  • Each subsequent row is a training sample with a class and corresponding text
  • No missing values or malformed CSV syntax

内容的提问来源于stack exchange,提问作者Octav Zlatior

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:43:21