{"id":11215,"library":"knitwork","title":"Knitwork JavaScript Code Generation Utilities","description":"Knitwork is a utility library designed for programmatic generation of JavaScript and TypeScript code. It provides functions to construct various language constructs, including ESM `import` and `export` statements, dynamic imports, and TypeScript-specific elements like type imports, interfaces, and module augmentations. The library also offers robust serialization utilities for converting JavaScript objects and arrays into string representations, with options for raw or escaped values, and string escaping functions. Currently at stable version 1.3.0, Knitwork is actively maintained within the unjs ecosystem, receiving regular updates with a focus on modern JavaScript practices (ESM-first) and TypeScript integration. Its key differentiator lies in offering granular control over code generation for tasks like bundler plugins, build-time optimizations, and code transformation, providing a lower-level API compared to full AST manipulation libraries.","status":"active","version":"1.3.0","language":"javascript","source_language":"en","source_url":"https://github.com/unjs/knitwork","tags":["javascript","typescript"],"install":[{"cmd":"npm install knitwork","lang":"bash","label":"npm"},{"cmd":"yarn add knitwork","lang":"bash","label":"yarn"},{"cmd":"pnpm add knitwork","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Knitwork is primarily an ESM-first package. While CJS usage is possible for some features, ESM is the recommended and best-supported import method.","wrong":"const { genImport } = require('knitwork')","symbol":"genImport","correct":"import { genImport } from 'knitwork'"},{"note":"All core utilities are named exports. There is no default export from the main package entry.","wrong":"import genObjectFromValues from 'knitwork'","symbol":"genObjectFromValues","correct":"import { genObjectFromValues } from 'knitwork'"},{"note":"Ensure named import for TypeScript-specific utilities like module augmentation.","wrong":"const genAugmentation = require('knitwork').genAugmentation","symbol":"genAugmentation","correct":"import { genAugmentation } from 'knitwork'"}],"quickstart":{"code":"import { genImport, genObjectFromValues, genInterface, genExport, genString } from 'knitwork';\n\n// Generate ESM imports\nconst importStatement = genImport('my-package', ['foo', { name: 'default', as: 'MyDefault' }]);\n// ~> import { foo, default as MyDefault } from \"my-package\";\n\n// Generate an object with escaped values\nconst dataObject = genObjectFromValues({\n  name: 'Knitwork Demo',\n  version: 1.0,\n  isActive: true,\n  config: {\n    url: 'https://example.com/api',\n    timeout: 5000\n  }\n});\n// ~> { name: \"Knitwork Demo\", version: 1, isActive: true, config: { url: \"https://example.com/api\", timeout: 5000 } }\n\n// Generate a TypeScript interface\nconst myInterface = genInterface('MyOptions', `{\n  id: string;\n  value: number;\n}`);\n// ~> interface MyOptions {\n//      id: string;\n//      value: number;\n//    }\n\n// Generate an export statement\nconst exportStatement = genExport('./types', { name: '*', as: 'MyTypes' });\n// ~> export * as MyTypes from \"./types\";\n\nconsole.log(importStatement + '\\n\\n' + dataObject + '\\n\\n' + myInterface + '\\n\\n' + exportStatement);\n","lang":"typescript","description":"Demonstrates generating various JavaScript and TypeScript code snippets, including imports, object serialization, interfaces, and exports using Knitwork's core utilities."},"warnings":[{"fix":"Use `import { someUtility } from 'knitwork';` instead of `const { someUtility } = require('knitwork');`.","message":"Knitwork is an ESM-first library. Direct `require()` statements for importing Knitwork utilities are not recommended and may lead to issues or require specific CommonJS transpilation setups. Always prefer ES module `import` syntax.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For objects containing string, number, or boolean values that need proper JavaScript serialization, use `genObjectFromValues`. Use `genObjectFromRaw` only when values are guaranteed to be valid, raw JavaScript expressions.","message":"When serializing objects, `genObjectFromRaw` does not escape or quote values, assuming they are raw code snippets. This can lead to invalid JavaScript if string values are passed directly. Use `genObjectFromValues` for general object serialization where values should be properly escaped and quoted.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade to Knitwork v1.1.0 or newer to ensure the `singleQuotes` option in `genString` is respected.","message":"Versions prior to v1.1.0 might not correctly respect the `singleQuotes` option in `genString`, potentially always outputting double quotes regardless of the setting. This was fixed in v1.1.0.","severity":"breaking","affected_versions":"<1.1.0"},{"fix":"Update Knitwork to version 1.3.0 or higher to correctly generate module augmentations without extra commas.","message":"Module augmentations generated with `genAugmentation` in versions prior to v1.3.0 might have included unnecessary commas, leading to syntax errors in some TypeScript environments. This was fixed in v1.3.0.","severity":"gotcha","affected_versions":"<1.3.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statements to use ES module syntax: `import { genImport } from 'knitwork';`","cause":"Attempting to use `require()` to import Knitwork in an ES module environment.","error":"ReferenceError: require is not defined in ES module scope"},{"fix":"Ensure you are using `import { functionName } from 'knitwork';` for named exports, and that your environment supports ES Modules or is correctly configured for transpilation if using CJS.","cause":"This error typically indicates an incorrect named import or a CommonJS context trying to access an ES module export incorrectly.","error":"TypeError: (0, knitwork.genImport) is not a function"},{"fix":"Run `npm install knitwork`, `yarn add knitwork`, or `pnpm add knitwork` to install the package. If the error persists, check your module resolution configuration.","cause":"The `knitwork` package is not installed or incorrectly resolved by your module system.","error":"Error: Cannot find module 'knitwork' or its corresponding type declarations."}],"ecosystem":"npm"}