http.request throws because the path has characters that must be escaped
ERR_UNESCAPED_CHARACTERSShort answer
Node.js refuses to send a request whose path contains characters that must be escaped, such as a space. The path is written into the HTTP request line as it is. Encode each value with encodeURIComponent, or build the address with the URL and URLSearchParams classes and let them do the escaping.
Fix it fast
1Find out which cause you have
Print the exact path you pass to the request, with quotes around it, so spaces and stray characters show.
console.log(JSON.stringify(options.path));2Match what you see, then jump to its fix
What does the error look like?
ERR_UNESCAPED_CHARACTERShttp.request throws before any request is sent
Cause 1 of 2
A user value or name with spaces is pasted into the path
1Check
The Node.js docs say an exception is thrown when the request path has illegal characters, and that currently spaces are rejected.
node -e "console.log(JSON.stringify('/users/' + 'Ann Lee'))"2Fix
Encode every dynamic piece before joining it into the path.
const path = '/search?q=' + encodeURIComponent(term);
http.request({ host: 'example.com', path }, (res) => res.resume()).end();3Verify
Print the path again and send the request.
node -e "console.log('/search?q=' + encodeURIComponent('hello world'))"Confirmed by nodejs.org
Cause 2 of 2
The address is assembled by string concatenation
1Check
Hand-built query strings are easy to get wrong. The URL class percent-encodes invalid characters in the path, and URLSearchParams serializes query values.
node -e "const u=new URL('https://example.org/a b'); console.log(u.pathname)"2Fix
Build the address with URL, set query values through searchParams, and pass the finished URL to the request.
const url = new URL('https://api.example.com/search');
url.searchParams.set('q', term);
https.get(url, (res) => res.resume());3Verify
Print url.href before sending.
node -e "const u=new URL('https://api.example.com/search'); u.searchParams.set('q','a b'); console.log(u.href)"Confirmed by nodejs.org
How do I stop it coming back?
- Never concatenate raw user input into a URL; use URL and searchParams.
- Log the final URL at debug level so a bad character is visible when the error appears.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix