Registry / serialization / knitwork

knitwork

JSON →
library1.3.0jsnpmunverified

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.

npm install knitwork
INSTALL
IMPORT
SIG · KNITWORK
K
knitwork
serializationjavascriptv1.3.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

genImport
import { genImport } from 'knitwork'
const { genImport } = require('knitwork')
Knitwork is primarily an ESM-first package. While CJS usage is possible for some features, ESM is the recommended and best-supported import method.
genObjectFromValues
import { genObjectFromValues } from 'knitwork'
import genObjectFromValues from 'knitwork'
All core utilities are named exports. There is no default export from the main package entry.
genAugmentation
import { genAugmentation } from 'knitwork'
const genAugmentation = require('knitwork').genAugmentation
Ensure named import for TypeScript-specific utilities like module augmentation.

Demonstrates generating various JavaScript and TypeScript code snippets, including imports, object serialization, interfaces, and exports using Knitwork's core utilities.

import { genImport, genObjectFromValues, genInterface, genExport, genString } from 'knitwork'; // Generate ESM imports const importStatement = genImport('my-package', ['foo', { name: 'default', as: 'MyDefault' }]); // ~> import { foo, default as MyDefault } from "my-package"; // Generate an object with escaped values const dataObject = genObjectFromValues({ name: 'Knitwork Demo', version: 1.0, isActive: true, config: { url: 'https://example.com/api', timeout: 5000 } }); // ~> { name: "Knitwork Demo", version: 1, isActive: true, config: { url: "https://example.com/api", timeout: 5000 } } // Generate a TypeScript interface const myInterface = genInterface('MyOptions', `{ id: string; value: number; }`); // ~> interface MyOptions { // id: string; // value: number; // } // Generate an export statement const exportStatement = genExport('./types', { name: '*', as: 'MyTypes' }); // ~> export * as MyTypes from "./types"; console.log(importStatement + '\n\n' + dataObject + '\n\n' + myInterface + '\n\n' + exportStatement);
Debug
Known issues
gotchaKnitwork 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.
fix
Use `import { someUtility } from 'knitwork';` instead of `const { someUtility } = require('knitwork');`.
affects: >=1.0.0
gotchaWhen 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.
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.
affects: >=1.0.0
breakingVersions 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.
fix
Upgrade to Knitwork v1.1.0 or newer to ensure the `singleQuotes` option in `genString` is respected.
affects: <1.1.0
gotchaModule 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.
fix
Update Knitwork to version 1.3.0 or higher to correctly generate module augmentations without extra commas.
affects: <1.3.0
Errors
Common errors & fixes
ReferenceError: require is not defined in ES module scope
Attempting to use `require()` to import Knitwork in an ES module environment.
fix
Change your import statements to use ES module syntax: `import { genImport } from 'knitwork';`
TypeError: (0, knitwork.genImport) is not a function
This error typically indicates an incorrect named import or a CommonJS context trying to access an ES module export incorrectly.
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.
Error: Cannot find module 'knitwork' or its corresponding type declarations.
The `knitwork` package is not installed or incorrectly resolved by your module system.
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.
Upgrade
Version history
1.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
knitwork — npm install knitwork · libregistry