jackwener/OpenCLI · error · ArgumentError
Media path cannot be empty
Error message
Media path cannot be empty
What it means
validateMixedMediaItems throws this ArgumentError when a media entry resolves to an empty string. Each --media item is trimmed and passed through path.resolve; if the input string is empty/falsy after trimming, path.resolve('') would return the process CWD, so the library explicitly checks !resolved and rejects it to avoid silently posting the working directory.
Source
Thrown at clis/instagram/post.js:97
})()
`;
}
function requirePage(page) {
if (!page)
throw new CommandExecutionError('Browser session required for instagram post');
return page;
}
function validateMixedMediaItems(inputs) {
if (!inputs.length) {
throw new ArgumentError('Argument "media" is required.', 'Provide --media /path/to/file.jpg or --media /path/a.jpg,/path/b.mp4');
}
if (inputs.length > MAX_MEDIA_ITEMS) {
throw new ArgumentError(`Too many media items: ${inputs.length}`, `Instagram carousel posts support at most ${MAX_MEDIA_ITEMS} items`);
}
const items = inputs.map((input) => {
const resolved = path.resolve(String(input || '').trim());
if (!resolved) {
throw new ArgumentError('Media path cannot be empty');
}
if (!fs.existsSync(resolved)) {
throw new ArgumentError(`Media file not found: ${resolved}`);
}
const ext = path.extname(resolved).toLowerCase();
if (SUPPORTED_IMAGE_EXTENSIONS.has(ext)) {
return { type: 'image', filePath: resolved };
}
if (SUPPORTED_VIDEO_EXTENSIONS.has(ext)) {
return { type: 'video', filePath: resolved };
}
throw new ArgumentError(`Unsupported media format: ${ext}`, 'Supported formats: images (.jpg, .jpeg, .png, .webp) and videos (.mp4)');
});
return items;
}
function normalizePostMediaItems(kwargs) {
const media = String(kwargs.media ?? '').trim();
return validateMixedMediaItems(media.split(',').map((part) => part.trim()).filter(Boolean));View on GitHub (pinned to 49907e53dc)
Solutions
- Remove empty entries from the media list before calling the command (filter out blank strings after splitting on commas).
- Interpolate path variables safely — use template literals or path.join instead of string concatenation with possibly-undefined values.
- Check that every environment/config variable supplying a path is set and non-blank.
- Pre-validate each entry with a small guard in your own code and skip or report empty ones.
- If calling the validator programmatically, normalize input the same way normalizePostMediaItems does (split, trim, filter(Boolean)).
Example fix
// before
const media = [baseDir + sep + name, extraPath].join(','); // name may be undefined -> empty entry
await run(['instagram', 'post', '--media', media]);
// after
const media = [name && path.join(baseDir, name), extraPath].filter(Boolean).join(',');
if (!media) throw new Error('No valid media paths');
await run(['instagram', 'post', '--media', media]); Defensive patterns
Strategy: validation
Validate before calling
const items = mediaArg.split(',').map(s => s.trim()).filter(Boolean);
items.forEach((item, i) => {
if (!item) throw new Error(`Media entry #${i + 1} is empty`);
if (!item.trim()) throw new Error(`Media entry #${i + 1} is whitespace-only`);
}); Type guard
function isNonEmptyPath(entry) {
return typeof entry === 'string' && entry.trim().length > 0;
} Try / catch
try {
await cli.run('instagram post', { media: mediaArg });
} catch (e) {
if (e instanceof ArgumentError && e.message === 'Media path cannot be empty') {
console.error('A media list entry was empty — remove blank entries between commas');
} else throw e;
} Prevention
- Always .split(',').map(trim).filter(Boolean) before building --media
- Never concatenate paths with possibly-undefined variables — use path.join
- Validate config/env values are set before interpolating into path lists
- Avoid hand-written lists with trailing/double commas like "a.jpg,,b.jpg"
- Normalize input the same way normalizePostMediaItems does before calling validators directly
When it happens
Trigger: In practice this fires when an item in the media list is an empty/whitespace-only entry that survives the split/filter (e.g. a path built by joining strings where one segment was empty and the trim/resolve combination yields falsy input), or when validateMixedMediaItems is called directly with [''].
Common situations: Template/config strings like "a.jpg,,b.jpg" with stray empty entries; script string concatenation producing "dir/" + undefined; paths supplied via environment variables that are unset for some entries; programmatic use of the exported validator with unnormalized input.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Argument "media" is required.
- Unknown 12306 station telecode "${trimmed}"
- Unknown 12306 station "${trimmed}"
- date must be YYYY-MM-DD, got "${value}"
- date "${value}" is not a real calendar date
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/c7e0a7c8da0fd991.
Report an issue: GitHub.