{"id":25107,"library":"compatfactory","title":"compatfactory","description":"compatfactory v4.0.4 is a TypeScript library that unifies the TypeScript Compiler API factory functions across all TypeScript versions (3.x, 4.x, 5.x) and makes them conform with the Node Factory API. It solves the problem of different method signatures and availability across TypeScript versions, especially after the removal of old factory functions in TS 5.0. The library provides a single `ensureNodeFactory` function that accepts a `NodeFactory` or a `typescript` module and returns a normalized `NodeFactory` compatible with the latest API. It is actively maintained with frequent updates to support new TypeScript versions (e.g., v5.6, v5.5, v5.3 in v4.0.0). Key differentiators: simple wrapper, tiny footprint, supports all TypeScript versions in one package. Requires Node >=18.20.0.","status":"active","version":"4.0.4","language":"javascript","source_language":"en","source_url":"https://github.com/wessberg/compatfactory","tags":["javascript","typescript","NodeFactory","Compiler API","compat","normalize"],"install":[{"cmd":"npm install compatfactory","lang":"bash","label":"npm"},{"cmd":"yarn add compatfactory","lang":"bash","label":"yarn"},{"cmd":"pnpm add compatfactory","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM-only since v4 (Node >=18.20); use ESM imports. For CommonJS, consider dynamic import.","wrong":"const ensureNodeFactory = require('compatfactory')","symbol":"ensureNodeFactory","correct":"import { ensureNodeFactory } from 'compatfactory'"},{"note":"TypeScript type import only; no runtime value.","symbol":"NodeFactory (type)","correct":"import type { NodeFactory } from 'compatfactory'"},{"note":"Default export is deprecated as of v4; use named `ensureNodeFactory` instead.","wrong":"import { default as compatfactory } from 'compatfactory'","symbol":"default export (deprecated)","correct":"import compatfactory from 'compatfactory'"}],"quickstart":{"code":"import { ensureNodeFactory } from 'compatfactory';\nimport ts from 'typescript';\n\n// Create a source file for demonstration\nconst sourceFile = ts.createSourceFile(\n  'test.ts',\n  'const x: number = 1',\n  ts.ScriptTarget.Latest\n);\n\n// Get normalized NodeFactory\nconst factory = ensureNodeFactory(ts);\n\n// Use unified factory API (works across TS versions)\nconst newImport = factory.createImportDeclaration(\n  undefined,\n  factory.createImportClause(\n    false,\n    undefined,\n    factory.createNamedImports([\n      factory.createImportSpecifier(\n        false,\n        undefined,\n        factory.createIdentifier('foo')\n      )\n    ])\n  ),\n  factory.createStringLiteral('./foo')\n);\n\nconsole.log(ts.createPrinter().printNode(ts.EmitHint.Unspecified, newImport, sourceFile));\n// Output: import { foo } from \"./foo\"","lang":"typescript","description":"Demonstrates using ensureNodeFactory to create a normalized NodeFactory, then creating an import declaration with a unified API."},"warnings":[{"fix":"Upgrade Node to >=18.20.0","message":"Node 16 and 17 support dropped","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Use named import: import { ensureNodeFactory } from 'compatfactory'","message":"Default export no longer works in v4","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Switch to named export ensureNodeFactory","message":"Default export deprecated","severity":"deprecated","affected_versions":">=4.0.0"},{"fix":"Cache the factory if called frequently; avoid repeated calls in hot paths","message":"ensureNodeFactory with older TypeScript versions may return a wrapped factory; performance can differ","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use import { ensureNodeFactory } from 'compatfactory' in an ESM context (package.json with \"type\": \"module\")","cause":"ESM-only package imported via require() or missing type module in package.json","error":"Cannot find module 'compatfactory' or its corresponding type declarations."},{"fix":"Use named import: import { ensureNodeFactory } from 'compatfactory'","cause":"Using default import instead of named import","error":"TypeError: ensureNodeFactory is not a function"},{"fix":"Convert to ESM imports or use dynamic import: await import('compatfactory')","cause":"Mixing require() with ESM package","error":"ReferenceError: exports is not defined in ES module scope"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}