mochajs/mocha · error · Error
Not enough non-option arguments: got 0, need at least 1
Error message
Not enough non-option arguments: got 0, need at least 1
What it means
The `mocha init` command requires exactly one positional argument: the destination path where the client-side Mocha setup (browser test scaffolding) will be created. The public `parse` function in lib/cli/init.js destructures the first argument and throws this error when no path argument was supplied, so it cannot know where to write the files.
Source
Thrown at lib/cli/init.js:18
/**
* Command module for "init" command
*
* @private
* @module
*/
import fs from "node:fs";
import path from "node:path";
export const command = "init <path>";
export const description = "create a client-side Mocha setup at <path>";
export const parse = (args) => {
const [pathArg] = args;
if (!pathArg) {
throw new Error("Not enough non-option arguments: got 0, need at least 1");
}
return { _: [], path: path.normalize(pathArg) };
};
export const handler = (argv) => {
const destdir = argv.path;
const srcdir = path.join(import.meta.dirname, "..", "..");
fs.mkdirSync(destdir, { recursive: true });
const css = fs.readFileSync(path.join(srcdir, "mocha.css"));
const js = fs.readFileSync(path.join(srcdir, "mocha.js"));
const tmpl = fs.readFileSync(
path.join(srcdir, "lib", "browser", "template.html"),
);
fs.writeFileSync(path.join(destdir, "mocha.css"), css);
fs.writeFileSync(path.join(destdir, "mocha.js"), js);
fs.writeFileSync(path.join(destdir, "tests.spec.js"), "");
fs.writeFileSync(path.join(destdir, "index.html"), tmpl);
};View on GitHub (pinned to 6bcbee4fd9)
Solutions
- Pass the destination directory: `mocha init ./tests` (or `mocha init <path>`).
- Check shell variable expansion/quoting — echo the command to confirm the path argument is actually present.
- If invoking parse() programmatically, pass `parse(['./my/path'])` with at least one string element.
Example fix
// before $ mocha init // Error: Not enough non-option arguments: got 0, need at least 1 // after $ mocha init ./test-browser
Defensive patterns
Strategy: validation
Validate before calling
const args = process.argv.slice(2);
if (args.length < 1 || args[0].startsWith('-')) {
console.error('Usage: mocha init <path>');
process.exit(1);
} Type guard
const hasPathArg = (args) => Array.isArray(args) && typeof args[0] === 'string' && args[0].length > 0;
Try / catch
try {
init.parse(process.argv.slice(3));
} catch (err) {
if (err.message.startsWith('Not enough non-option arguments')) {
console.error('Usage: mocha init <path>');
process.exit(1);
}
throw err;
} Prevention
- Always pass a destination path to `mocha init`.
- Wrap CLI invocations in scripts that assert argument counts before exec.
- Beware unset shell variables in CI: use `${VAR:?msg}` to fail loudly.
- Check `mocha init --help` for expected usage.
When it happens
Trigger: Running `mocha init` (or `mocha init --reporter spec` etc.) with zero non-option arguments; also `parse([])` or `parse(['--some-option'])` called programmatically with no positional.
Common situations: Developers forgetting the output directory when bootstrapping browser tests; shell quoting issues that swallow the argument (e.g. empty variable expansion like `mocha init $UNSET_PATH`); migrating CI scripts where the path argument was dropped.
Related errors
- Not enough arguments following: ${name}
- ERR_MOCHA_UNSUPPORTED
- ERR_MOCHA_MISSING_ARGUMENT
- Missing runner argument
- Missing runner argument
AI-assisted analysis of mochajs/mocha@6bcbee4fd9 (2026-09-01).
Data as JSON: /api/errors/760177c6e7eeac49.
Report an issue: GitHub.