{"id":17335,"library":"prepend-http","title":"Prepend HTTP/S Protocol to URLs","description":"prepend-http is a focused utility package designed to ensure URLs have a proper `http://` or `https://` protocol prefix. It intelligently adds `https://` (by default) to \"humanized\" URLs such as domain names (`sindresorhus.com`) or `localhost` entries, while leaving already-prefixed URLs untouched. The package is currently stable at version `4.0.0`, released recently in response to ecosystem changes. Historically, releases are infrequent but significant, often tied to Node.js LTS updates or module system shifts. A key differentiator is its simplicity and reliability, provided by a well-regarded maintainer in the JavaScript ecosystem. Since version 4, prepend-http is a pure ESM package, aligning with modern JavaScript module practices, and requires Node.js 12.20 or newer.","status":"active","version":"4.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/sindresorhus/prepend-http","tags":["javascript","prepend","protocol","scheme","url","http","https","humanized"],"install":[{"cmd":"npm install prepend-http","lang":"bash","label":"npm"},{"cmd":"yarn add prepend-http","lang":"bash","label":"yarn"},{"cmd":"pnpm add prepend-http","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v4.0.0, this package is pure ESM. CommonJS `require()` is no longer supported and will result in an `ERR_REQUIRE_ESM` error.","wrong":"const prependHttp = require('prepend-http');","symbol":"prependHttp","correct":"import prependHttp from 'prepend-http';"},{"note":"The type definition for the `options` object is typically exported as `Options`. Use `import type` for type-only imports to prevent potential bundling issues or unnecessary code inclusion.","wrong":"import { Options } from 'prepend-http';","symbol":"Options","correct":"import type { Options } from 'prepend-http';"}],"quickstart":{"code":"import prependHttp from 'prepend-http';\n\n// Example 1: Basic usage - prepends https:// by default\nconst url1 = 'sindresorhus.com';\nconst prefixedUrl1 = prependHttp(url1);\nconsole.log(`'${url1}' -> '${prefixedUrl1}'`);\n// Expected: 'sindresorhus.com' -> 'https://sindresorhus.com'\n\n// Example 2: Explicitly prepend http://\nconst url2 = 'localhost:3000';\nconst prefixedUrl2 = prependHttp(url2, { https: false });\nconsole.log(`'${url2}' -> '${prefixedUrl2}'`);\n// Expected: 'localhost:3000' -> 'http://localhost:3000'\n\n// Example 3: URL already has a protocol - should be untouched\nconst url3 = 'ftp://example.com/file.txt';\nconst prefixedUrl3 = prependHttp(url3);\nconsole.log(`'${url3}' -> '${prefixedUrl3}'`);\n// Expected: 'ftp://example.com/file.txt' -> 'ftp://example.com/file.txt'\n\n// Example 4: Mixed case - ensure consistency\nconst url4 = 'http://github.com';\nconst prefixedUrl4 = prependHttp(url4);\nconsole.log(`'${url4}' -> '${prefixedUrl4}'`);\n// Expected: 'http://github.com' -> 'http://github.com'","lang":"typescript","description":"Demonstrates how to use `prependHttp` with various URL inputs, including defaulting to `https`, explicitly using `http`, and handling already-prefixed URLs."},"warnings":[{"fix":"Update all imports to use ESM `import` syntax (e.g., `import prependHttp from 'prepend-http';`). Ensure your Node.js project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json` or using `.mjs` file extensions).","message":"Starting with v4.0.0, `prepend-http` is a pure ES Module (ESM). CommonJS `require()` statements will no longer work and will throw an `ERR_REQUIRE_ESM` error.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Upgrade your Node.js runtime to version 12.20.0, 14.13.1, or any version 16.0.0 or newer.","message":"Version 4.0.0 requires Node.js 12.20.0 or higher (or 14.13.1 or >=16.0.0). Older Node.js versions are no longer supported.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"If you require `http://` by default, explicitly pass `{ https: false }` as the second argument: `prependHttp(url, { https: false })`.","message":"The `https` option now defaults to `true` since v3.0.0. Previously, it defaulted to `false` in versions prior to 3.0.0, meaning `prependHttp('example.com')` would have resulted in `http://example.com`.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Upgrade your Node.js runtime to version 8 or higher. (Note: v4.0.0 requires an even newer Node.js version, see other warnings).","message":"Version 3.0.0 required Node.js 8 or higher. Older Node.js versions were no longer supported from this version onwards.","severity":"breaking","affected_versions":">=3.0.0 <4.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Change `const prependHttp = require('prepend-http');` to `import prependHttp from 'prepend-http';`. Ensure your environment supports ESM (e.g., `\"type\": \"module\"` in `package.json`).","cause":"Attempting to use CommonJS `require()` to import `prepend-http`, which is a pure ESM package since v4.0.0.","error":"ERR_REQUIRE_ESM"},{"fix":"Ensure you are using `import prependHttp from 'prepend-http';` in an ESM context. If you must `require` from a CJS file, consider using dynamic `import()`: `const { default: prependHttp } = await import('prepend-http');` (requires `async/await`).","cause":"This error can occur if an ES Module (ESM) is incorrectly imported into a CommonJS (CJS) context, or if the default export is not correctly accessed when dynamically importing ESM into CJS.","error":"TypeError: prependHttp is not a function"},{"fix":"Replace `const prependHttp = require('prepend-http');` with `import prependHttp from 'prepend-http';` and ensure all other dependencies are imported using `import` statements.","cause":"Attempting to use `require()` in an ES Module context (e.g., in a file where `\"type\": \"module\"` is set in `package.json` or the file has a `.mjs` extension).","error":"ReferenceError: require is not defined"}],"ecosystem":"npm","meta_description":null}