{"id":25543,"library":"esmod","title":"esmod","description":"esmod is a fast in-place transpiler for converting ES6 import/export statements to CommonJS require/exports. Version 1.0.0 is currently stable, with a focus on speed by leveraging AST-based transformation rather than regex-only approaches. It distinguishes itself from tools like esbuild or Babel by being designed for lightweight, single-file transformations without module resolution or bundling — ideal for build tool authors and custom pipelines.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/rreverser/esmod","tags":["javascript","es6","module","import","export","transpiler","fast","ast"],"install":[{"cmd":"npm install esmod","lang":"bash","label":"npm"},{"cmd":"yarn add esmod","lang":"bash","label":"yarn"},{"cmd":"pnpm add esmod","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM-only package; CommonJS require() does not work natively unless in a Node ESM context.","wrong":"const transform = require('esmod').transform","symbol":"transform","correct":"import { transform } from 'esmod'"},{"note":"transformSync is a named export, not a default export.","wrong":"import transformSync from 'esmod'","symbol":"transformSync","correct":"import { transformSync } from 'esmod'"},{"note":"The default export is a shorthand for the transform function.","wrong":"const { transform } = require('esmod')","symbol":"default transform","correct":"import transform from 'esmod'"}],"quickstart":{"code":"import { transform } from 'esmod';\nimport { readFileSync, writeFileSync } from 'fs';\n\nconst code = readFileSync('input.mjs', 'utf8');\nconst result = transform(code, { from: 'esm', to: 'cjs' });\nwriteFileSync('output.cjs', result.code);\nconsole.log('Transformed successfully!');","lang":"typescript","description":"Converts an ES module file (.mjs) to CommonJS (.cjs) using esmod's transform function with explicit source and target formats."},"warnings":[{"fix":"Wrap in try/catch (not .catch()) since it's not a Promise.","message":"transform() throws synchronous errors; do not use try/catch with async error patterns.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure code is pure ES module syntax before passing to transform.","message":"Input code must contain only valid ES module syntax; mixing require() and import will cause parse errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always specify both targets to avoid accidental no-op.","message":"options.from and options.to are optional; default is ESM-to-ESM (no-op), which may be unexpected.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Access result.code immediately after calling transform() without await.","cause":"Calling transform() without waiting for a Promise, but it's actually synchronous and returns an object directly.","error":"TypeError: Cannot read properties of undefined (reading 'code')"},{"fix":"Set { from: 'esm' } in options.","cause":"Input contains export statements but options.from is not set to 'esm', so transform treats input as CommonJS.","error":"SyntaxError: Unexpected token 'export'"},{"fix":"Set 'to' to 'cjs' to convert to CommonJS.","cause":"Both from and to options are set to 'esm', resulting in no transformation.","error":"Error: No transform applied because source and target are the same."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}