Install and run

Choose how the command is available.

UseCommandWhat it does
Global commandnpm install --global node-http-serverAdds node-http-server to the active npm environment.
One-off runnpx node-http-server --root ./publicRuns the package command without relying on a global install.
Repository scriptnpm start -- --root ./publicRuns this repository's CLI entry and forwards flags after --.
shell · local static root
node-http-server --root ./public --port 8080
Expected output
node-http-server listening at http://127.0.0.1:8080

The printed URL uses the actual listener address and port. With --port 0, the operating system selects a free port and that assigned port appears in the output.

Common tasks

Copy the command for the job.

TaskCommandExpected behavior
Serve current directorynode-http-serverLocal HTTP on 127.0.0.1:8080.
Serve another directorynode-http-server --root ./publicPaths resolve from the current working directory.
Pick a local portnode-http-server --port 9000Local HTTP on port 9000.
Pick a free portnode-http-server --port 0Prints the operating-system-assigned port.
Accept network trafficnode-http-server --host 0.0.0.0Binds all IPv4 interfaces; firewall and network policy still apply.
Require one Host valuenode-http-server --domain example.testOther Host values receive 421; this does not set DNS or the bind address.
Serve an SPAnode-http-server --root ./dist --spa --compressionMissing extensionless HTML routes use the index; eligible responses negotiate compression.
Use another SPA filenode-http-server --spa=app.htmlUses app.html as the fallback.
Limit bodies to 1 MiBnode-http-server --max-body 1048576Oversized request bodies stop with 413.
Disable one timeoutnode-http-server --request-timeout falsefalse, off, and 0 disable numeric timeout/body controls.
Allow client cachingnode-http-server --cacheTurns off the default no-cache directives.
Serve intentional dotfilesnode-http-server --root ./public --allow-dotfilesAllows every dot-prefixed segment in that root, including /.well-known; audit the root first.
Write request logsnode-http-server --log ./logs/requests.ndjsonAppends one JSON record per line; the parent directory must exist.
Inspect activitynode-http-server --verbosePrints normalized configuration and listener activity.
SPA filename syntax: use --spa=app.html. With --spa app.html, app.html becomes the positional root when no root is set, or becomes an unknown argument. --spa=false means a fallback file literally named false; it does not disable SPA fallback.

Complete option reference

Every documented flag.

OptionDefaultSetsAccepted value / note
-p, --port <port>8080portNonnegative number; deployment requires an integer from 0–65535.
-r, --root <path>current directoryrootExisting directory, resolved from the working directory.
--host <address>127.0.0.1hostNonempty listen address.
--domain <hostname>0.0.0.0domainPrimary accepted Host; not a network bind or DNS setting.
--index <file>index.htmlserver.indexDirectory index filename; there is no directory listing when it is missing.
--no-cacheonserver.noCache=trueExplicitly sends default no-cache directives.
--cacheoffserver.noCache=falseOmits the default no-cache header; it does not add a cache lifetime.
--allow-dotfilesoffserver.allowDotfiles=trueExplicit all-dotfile opt-in; --allow-dotfiles=false restores the default denial.
--spa[=<file>]offserver.spaFallbackNo value uses the index; custom filename requires =.
--compressionoffserver.compression=trueStatic eligible responses at least 1024 bytes; not byte ranges.
--max-body <bytes|false>unlimitedserver.maxRequestBodyBytesNonnegative bytes; false, off, or 0 is unlimited.
--timeout <ms|false>30000server.timeoutSocket inactivity milliseconds; false/off/0 disables.
--request-timeout <ms|false>300000server.requestTimeoutWhole-request milliseconds; false/off/0 disables.
--headers-timeout <ms|false>60000server.headersTimeoutHeader milliseconds; false/off/0 disables.
--keep-alive-timeout <ms|false>5000server.keepAliveTimeoutKeep-alive milliseconds; false/off/0 disables.
--log <path>offlogNDJSON append destination.
-v, --verboseoffverbose=truePrints operational details.
-h, --helphelp outputParses all input first, then prints help and exits 0.
--versionversion outputPrints the package version and exits 0. Help wins when both are set.

Input syntax

Modern flags and compatible input.

FormExampleParsing rule
Long option + value--root ./publicThe next token supplies a value for value-taking options.
Long option + equals--port=9000Equivalent to the separated form.
Short option-p 9000-p, -r, -v, and -h are supported.
Positional rootnode-http-server ./publicThe first bare token becomes root; later bare tokens fail.
v8 key=valueroot=./public port=9000 verbose=trueCompatibility form; sequential inputs are last-wins.
Legacy camelCase aliasesrequestTimeout=falsenoCache, allowDotfiles, spaFallback, maxRequestBodyBytes, requestTimeout, headersTimeout, and keepAliveTimeout map to modern options.
Ordering: options are processed from left to right; the last value for the same setting wins. All arguments are validated before help or version output.
Parsing details: boolean words are lowercase true/false. A flag needs equals to turn it off—for example --compression=false, not --compression false. Short options cannot be bundled. If a value starts with -, use equals, such as --root=-name. --cache=<anything> still enables caching.

Output and process exits

Use exit status as the contract.

ConditionOutputExit status
Listener readynode-http-server listening at http://ADDRESS:PORTProcess remains running.
Help or versionRequested information on stdout.0
Argument parse errorError plus Run node-http-server --help for usage.2
Deploy/listener/close failureError message on stderr; sibling listeners close after listener failure.1
Request-level 4xx/5xx or log-write failureThe request completes; verbose mode reports asynchronous log failures.Server keeps running.
SIGINT / Ctrl+CListeners close.130
SIGTERMListeners close.0
shell · verify inputs without starting
node-http-server --help
node-http-server --version

Troubleshooting

Read the first failure literally.

SymptomLikely causeNext check
EADDRINUSEAnother process owns the port.Choose another --port or stop the conflicting process.
EACCESThe address/port is not permitted.Use an allowed unprivileged port and confirm host permissions.
root must be a directoryThe path is missing or is a file.Resolve the root from the current working directory.
421 Misdirected RequestThe Host header does not match domain.Fix the request Host or remove/change --domain.
Remote machine cannot connectLoopback bind, firewall, or wrong address.Set --host 0.0.0.0 deliberately, allow the port, then connect to the machine's network address—not 0.0.0.0.
SPA file not selectedThe custom file was separated by a space.Use --spa=app.html.
Dot-prefixed path returns 403Dotfiles are blocked by default.Move the public file to a normal path, or use --allow-dotfiles only with a dedicated audited root.
Log errors appear laterParent directory missing or write denied.Create/protect the directory and use --verbose to surface asynchronous failures.
Unknown option: configThe CLI has no JSON config loader.Use the module API; a bare config.json is treated as the root path.

CLI boundary

Use the library for object configuration.

The CLI intentionally covers common static-server operations. It does not load configuration JSON or expose every nested object.

Needs the module APIConfiguration/API link
HTTPS keys and certificateshttps configuration
Multiple domain rootsdomains mapping
MIME overrides, rejected/restricted extensionscontentType and restrictedType
Custom error bodies/headerserrors
Compression thresholdserver.compressionThreshold
Log bodies or custom logger functionsLogging configuration
Hooks, multiple instances, lifecycle controlLibrary API