{"id":10526,"library":"autolinker","title":"Autolinker.js","description":"Autolinker.js is a robust JavaScript utility designed to automatically detect and convert various patterns like URLs, email addresses, phone numbers, and social media mentions/hashtags (Twitter, Instagram, YouTube, TikTok, Facebook, Soundcloud) within plain text or HTML into clickable hyperlinks. The current stable version is `v4.1.5`. The library maintains an active release cadence, frequently publishing minor and patch updates, often focusing on performance enhancements and bug fixes. A key differentiator is its rewritten URL parser in `v4.0.0`, which utilizes a finite state machine for `O(n)` linear time performance, significantly improving speed and preventing 'catastrophic backtracking' issues common with regular expression-based parsers. Unlike many alternatives, Autolinker.js is meticulously designed to correctly handle HTML input, avoiding the creation of nested anchor tags or inadvertently modifying existing `href` attributes, making it particularly suitable for processing rich text content where correctness and performance are critical.","status":"active","version":"4.1.5","language":"javascript","source_language":"en","source_url":"git://github.com/gregjacobs/Autolinker.js","tags":["javascript","auto","link","autolink","url","urls","anchor","typescript"],"install":[{"cmd":"npm install autolinker","lang":"bash","label":"npm"},{"cmd":"yarn add autolinker","lang":"bash","label":"yarn"},{"cmd":"pnpm add autolinker","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary `Autolinker` class is exported as a default export since v2. Using named import is incorrect.","wrong":"import { Autolinker } from 'autolinker';","symbol":"Autolinker","correct":"import Autolinker from 'autolinker';"},{"note":"For CommonJS environments (Node.js), the package should be `require`d and typically aliased with a capital letter as it's a class.","wrong":"const autolinker = require('autolinker');","symbol":"Autolinker (CommonJS)","correct":"const Autolinker = require('autolinker');"},{"note":"The `Match` interface/type for TypeScript is available as a named export. Ensure you also import `Autolinker` as the default export if needed.","wrong":"import { Match } from 'autolinker';","symbol":"Match","correct":"import Autolinker, { Match } from 'autolinker';"}],"quickstart":{"code":"import Autolinker from 'autolinker';\n\nconst inputText: string = \"Check out my website at example.com, email me at test@example.org, or call me at +1 (123) 456-7890. Also, find me on Twitter @myhandle and hashtag #myproject.\";\n\nconst autolinker = new Autolinker({\n  urls: { scheme: true, www: true, ipV4Matches: true },\n  email: true,\n  phone: true,\n  mention: 'twitter',\n  hashtag: 'twitter',\n  newWindow: true,\n  stripPrefix: false,\n  // Custom replace function to add specific classes or attributes\n  replaceFn: (match) => {\n    const tag = match.buildTag();\n    if (match.getType() === 'url') {\n      tag.setAttr('class', 'my-url-link');\n    }\n    return tag;\n  }\n});\n\nconst linkedText: string = autolinker.link(inputText);\n\nconsole.log(linkedText);\n// Expected output: Check out my website at <a href=\"http://example.com\" class=\"my-url-link\" target=\"_blank\" rel=\"noopener noreferrer\">example.com</a>, email me at <a href=\"mailto:test@example.org\" target=\"_blank\" rel=\"noopener noreferrer\">test@example.org</a>, or call me at <a href=\"tel:+11234567890\" target=\"_blank\" rel=\"noopener noreferrer\">+1 (123) 456-7890</a>. Also, find me on Twitter <a href=\"https://twitter.com/myhandle\" target=\"_blank\" rel=\"noopener noreferrer\">@myhandle</a> and hashtag <a href=\"https://twitter.com/hashtag/myproject\" target=\"_blank\" rel=\"noopener noreferrer\">#myproject</a>.","lang":"typescript","description":"This quickstart demonstrates how to initialize Autolinker with various options and link a block of text, including a custom `replaceFn` to modify the generated anchor tags."},"warnings":[{"fix":"Review the official breaking changes documentation for v4.x.x, especially regarding URL parsing behavior and the `Match` object API. Adjust code that relied on custom regex or direct `Match.type` access.","message":"Autolinker v4.0.0 introduced significant breaking changes, primarily a complete rewrite of the URL parser from regular expressions to a finite state machine. This change, while greatly improving performance and preventing ReDoS attacks, removes the ability to override regular expressions in `Matcher` classes. The `urls.wwwMatches` config was also removed, and `Match.getType()` replaced `Match.type`.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Always pass raw or pre-processed text/HTML directly to `Autolinker.link()` and rely on its internal HTML parsing for correct behavior. Avoid manually replacing parts of the string that might contain valid HTML tags or already linked content.","message":"When migrating from older versions or other autolinking libraries, be aware that Autolinker.js explicitly handles HTML input to prevent common issues like double-nested anchor tags or clobbering existing `href` attributes. Directly manipulating innerHTML or using other string replacement methods alongside Autolinker may lead to unexpected results.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Refactor custom linking logic to use the `replaceFn` option provided by `Autolinker` which offers a flexible way to modify generated links or even prevent linking for specific matches.","message":"The codebase was converted to TypeScript and now uses ES6 exports. Direct overriding of regular expressions by assigning to `Matcher.prototype` is no longer supported since v2, which may affect highly customized implementations.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"No direct fix within the library currently for this specific issue. Users dealing with text containing RTLO characters should implement external sanitization or validation if this is a critical concern, or monitor for future patches.","message":"Autolinker v3.16.2 reverted an attempted fix for Right-to-Left Override (RTLO) characters splitting links, meaning the library may not fully handle edge cases involving these characters and could result in improperly split links.","severity":"gotcha","affected_versions":"3.16.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For CommonJS, use `const Autolinker = require('autolinker');`. For ESM, use `import Autolinker from 'autolinker';`. Ensure your project's `package.json` `\"type\"` field and `tsconfig.json` `module` and `moduleResolution` options are correctly configured for your environment (e.g., `\"type\": \"module\"` for ESM, `\"type\": \"commonjs\"` for CJS).","cause":"Attempting to `require()` Autolinker in a CommonJS environment while the package is treated as an ES Module, or vice versa, due to Node.js module resolution issues.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported"},{"fix":"Change the import statement to `import Autolinker from 'autolinker';` for ESM/TypeScript environments. For CommonJS, ensure `const Autolinker = require('autolinker');` is used.","cause":"Trying to instantiate `Autolinker` as a class (`new Autolinker()`) but it was imported incorrectly, often due to using a named import `import { Autolinker } from 'autolinker';` instead of the default import.","error":"TypeError: Autolinker is not a constructor"},{"fix":"For browsers, include the `Autolinker.min.js` script directly via a `<script>` tag, which exposes `Autolinker` globally. For Node.js, ensure your `package.json` contains `\"type\": \"module\"` or use a `.mjs` file extension, or revert to CommonJS `require()` syntax.","cause":"Using `import Autolinker from 'autolinker';` in a non-ES module browser environment or a Node.js script not configured as an ES module.","error":"Uncaught SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm"}