jackwener/OpenCLI · error · ArgumentError

Not a valid image file: ${absPath}

Error message

Not a valid image file: ${absPath}

What it means

validateImagePaths throws ArgumentError when a path with a supported extension does not resolve to an existing regular file. fs.statSync(absPath, { throwIfNoEntry: false }) returns undefined for missing paths, and directories also fail the stat.isFile() check.

Source

Thrown at clis/xianyu/publish.js:72

    return condition;
}

function validateImagePaths(raw) {
    if (!raw) return [];
    const paths = String(raw).split(',').map((item) => item.trim()).filter(Boolean);
    if (paths.length === 0) return [];
    if (paths.length > MAX_IMAGES) {
        throw new ArgumentError(`xianyu publish images supports at most ${MAX_IMAGES} files`);
    }
    return paths.map((item) => {
        const absPath = path.resolve(item);
        const ext = path.extname(absPath).toLowerCase();
        if (!SUPPORTED_IMAGE_EXTENSIONS.has(ext)) {
            throw new ArgumentError(`Unsupported image format "${ext}". Supported: jpg, jpeg, png, webp`);
        }
        const stat = fs.statSync(absPath, { throwIfNoEntry: false });
        if (!stat || !stat.isFile()) {
            throw new ArgumentError(`Not a valid image file: ${absPath}`);
        }
        return absPath;
    });
}

function normalizePublishArgs(kwargs) {
    const price = parsePositivePrice(kwargs.price, 'price');
    if (price == null) {
        throw new ArgumentError('xianyu publish price cannot be empty');
    }
    const normalized = {};
    normalized.title = requireText(kwargs.title, 'title');
    normalized.description = requireText(kwargs.description, 'description');
    normalized.price = price;
    normalized.condition = validateCondition(kwargs.condition);
    normalized.category = requireText(kwargs.category, 'category');
    normalized.original_price = parsePositivePrice(kwargs.original_price, 'original_price');
    normalized.location = kwargs.location ? requireText(kwargs.location, 'location') : '';

View on GitHub (pinned to 49907e53dc)

Solutions

  1. fs.statSync each path and require isFile() before calling publish
  2. Use absolute paths or ensure the process cwd matches where relative paths are defined
  3. Confirm file names/spelling in the directory; correct or remove the bad entry

Example fix

// before
await publish({ images: './photos/product.jpg,./photos' }); // directory passed
// after
const images = ['./photos/product.jpg'].filter((p) => fs.statSync(p).isFile());
await publish({ images: images.join(',') });
Defensive patterns

Strategy: validation

Validate before calling

const fs = require('fs');
const path = require('path');
const checked = String(images).split(',').map((p) => path.resolve(p.trim())).filter(Boolean);
for (const p of checked) {
  const st = fs.statSync(p, { throwIfNoEntry: false });
  if (!st || !st.isFile()) throw new Error(`Not a valid image file: ${p}`);
}

Type guard

function isExistingFile(p) {
  const st = fs.statSync(path.resolve(p), { throwIfNoEntry: false });
  return Boolean(st && st.isFile());
}

Try / catch

try {
  await publish({ images });
} catch (e) {
  if (e instanceof ArgumentError && /Not a valid image file/.test(e.message)) {
    const bad = e.message.split(': ')[1];
    console.error(`Missing or not a file: ${bad}; check cwd and spelling`);
  } else throw e;
}

Prevention

When it happens

Trigger: Passing an images list where any entry resolves (via path.resolve) to a nonexistent path, deleted file, or directory, e.g. images: '/tmp/photos,/tmp/missing.jpg'.

Common situations: Relative paths resolved against an unexpected cwd, files deleted between listing and publish, typos in file names, empty comma entries producing odd paths, or passing a directory instead of files.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/2a568bef0a2290ff. Report an issue: GitHub.