API
- linkExists(link: string, config?: LinkValidatorConfig): Promise<boolean | LinkCheckResult>
+ linkExists(link: string, config?: LinkValidatorConfig): Promise<boolean | LinkCheckResult>
link — URL string to check.
config — optional object; all fields are optional.
- Throws TypeError only if link is not a string or a config field has the wrong type. Network problems return false (or a failure object when details: true).
+ Throws TypeError only if link is not a string or a config field has the wrong type.
+ Network problems return false (or a failure object when details: true).
Return values
Default (details off or false)
- Promise<boolean> — true if a response was received (any normal HTTP status). false for bad URLs, timeouts, missing fetch, or network failure.
+ Promise<boolean> — true if a response was received (any normal HTTP status).
+ false for bad URLs, timeouts, missing fetch, or network failure.
Detailed (details: true)
Promise<LinkCheckResult>:
- interface LinkCheckResult {
- exists: boolean; // same idea as response.ok
- status: number; // HTTP status, or 0 when there is no response
- url: string; // final response URL, or your URL string on failure
+ interface LinkCheckResult {
+ exists: boolean; // same idea as response.ok
+ status: number; // HTTP status, or 0 when there is no response
+ url: string; // final response URL, or your URL string on failure
}
@@ -203,28 +489,32 @@
TypeScript examples
- Compile to JavaScript first (for example tsc), then run with Node. Below shows typical terminal output.
+
+ Import the module and use the linkExists function. Here are some code examples.
+
Simple check
- import { linkExists } from 'link-exists';
+ import { linkExists } from 'link-exists';
-const ok = await linkExists('https://example.com');
-console.log(ok);
- $ node dist/app.js
+const ok = await linkExists('https://example.com');
+console.log(ok);
+ $ node app.js
true
Detailed result
- import { linkExists } from 'link-exists';
+ import { linkExists } from 'link-exists';
-const info = await linkExists('https://example.com', { details: true });
-console.log(JSON.stringify(info, null, 2));
- $ node dist/app.js
+const info = await linkExists('https://example.com', { details: true });
+console.log(JSON.stringify(info, null, 2));
+ $ node app.js
{
"exists": true,
"status": 200,
@@ -232,91 +522,100 @@ Detailed result
}
Timeout and method
- const ok = await linkExists('https://example.com', {
- timeout: 5000,
- method: 'GET',
+ const ok = await linkExists('https://example.com', {
+ timeout: 5000,
+ method: 'GET',
});
-console.log(ok);
- $ node dist/app.js
+console.log(ok);
+ $ node app.js
true
- Host-only or www. strings with no http:// or https:// return false by default. Use a full URL or set ignoreProtocol: true so http:// is added.
+ Host-only or www. strings with no http:// or https://
+ return false by default. Use a full URL or set ignoreProtocol: true so
+ http:// is added.
Host or www without a scheme (default)
- import { linkExists } from 'link-exists';
+ import { linkExists } from 'link-exists';
-const ok = await linkExists('www.stackblogger.com');
-console.log(ok);
- $ node dist/app.js
+const ok = await linkExists('www.stackblogger.com');
+console.log(ok);
+ $ node app.js
false
Same host with ignoreProtocol
- import { linkExists } from 'link-exists';
+ import { linkExists } from 'link-exists';
-const ok = await linkExists('www.stackblogger.com', { ignoreProtocol: true });
-console.log(ok);
- $ node dist/app.js
+const ok = await linkExists('www.stackblogger.com', { ignoreProtocol: true });
+console.log(ok);
+ $ node app.js
true
Bare domain with ignoreProtocol
- const ok = await linkExists('example.com', { ignoreProtocol: true });
-console.log(ok);
- $ node dist/app.js
+ const ok = await linkExists('example.com', { ignoreProtocol: true });
+console.log(ok);
+ $ node app.js
true
- JavaScript (CommonJS) examples
+ JavaScript examples
+
+
+ Require the module and use the linkExists function. Below shows typical terminal output.
+
Require and run
- const { linkExists } = require('link-exists');
+ const { linkExists } = require('link-exists');
-(async () => {
- const ok = await linkExists('https://example.com');
- console.log(ok);
+(async () => {
+ const ok = await linkExists('https://example.com');
+ console.log(ok);
})();
- $ node app.cjs
+ $ node app.js
true
- Host-only or www. without a scheme returns false unless you pass ignoreProtocol: true or use a full URL.
+ Host-only or www. without a scheme returns false unless you pass
+ ignoreProtocol: true or use a full URL.
Host or www without a scheme (default)
- const { linkExists } = require('link-exists');
-(async () => {
- console.log(await linkExists('www.stackblogger.com'));
+ const { linkExists } = require('link-exists');
+(async () => {
+ console.log(await linkExists('www.stackblogger.com'));
})();
- $ node app.cjs
+ $ node app.js
false
Same host with ignoreProtocol
- const { linkExists } = require('link-exists');
-(async () => {
- console.log(await linkExists('www.stackblogger.com', { ignoreProtocol: true }));
+ const { linkExists } = require('link-exists');
+(async () => {
+ console.log(await linkExists('www.stackblogger.com', { ignoreProtocol: true }));
})();
- $ node app.cjs
+ $ node app.js
true
Failure in CLI (boolean mode)
- const { linkExists } = require('link-exists');
-(async () => {
- console.log(await linkExists('https://this-host-should-not-resolve.test/'));
+ const { linkExists } = require('link-exists');
+(async () => {
+ console.log(await linkExists('https://this-host-should-not-resolve.test/'));
})();
- $ node app.cjs
+ $ node app.js
false
Detailed failure shape
- const { linkExists } = require('link-exists');
-(async () => {
- const r = await linkExists('not a real url .com', { details: true });
- console.log(JSON.stringify(r));
+ const { linkExists } = require('link-exists');
+(async () => {
+ const r = await linkExists('not a real url .com', { details: true });
+ console.log(JSON.stringify(r));
})();
- $ node app.cjs
+ $ node app.js
{"exists":false,"status":0,"url":"not a real url .com"}