Registry / devops / import-cost-core

import-cost-core

JSON →
library5.5.0jsnpmunverified

Calculate the bundle size of imported packages in JavaScript and TypeScript with a CLI and Node.js API. Version 5.5.0, actively maintained (2025 release). Powered by esbuild and es-module-lexer for high performance — scans 50+ files in less than a second. Provides minified, gzipped, and brotli sizes; identifies tree-shakeable packages; supports budget enforcement, JSON output, ignore patterns, watch mode, and git diff for CI/CD. Works with npm, pnpm, yarn, and bun. Aims to be a drop-in replacement for the deprecated import-cost VS Code extension backend, offering more accuracy and speed than webpack-bundle-analyzer or cost-of-modules.

npm install import-cost-core
INSTALL
IMPORT
SIG · IMPORT-COST-CORE
I
import-cost-core
devopsjavascriptv5.5.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

importCost
import { importCost } from 'import-cost-core'
Main function; returns an EventEmitter. Available since v1.
Lang
import { Lang } from 'import-cost-core'
Enum for file language: Lang.JAVASCRIPT, Lang.TYPESCRIPT, Lang.VUE, Lang.SVELTE.
PackageInfo
import type { PackageInfo } from 'import-cost-core'
import { PackageInfo } from 'import-cost-core'
PackageInfo is a TypeScript type/interface, not a runtime value. Must use 'import type'.
cleanup
import { cleanup } from 'import-cost-core'
Must be called on shutdown to terminate esbuild workers.

Demonstrates API usage: scanning a TypeScript file for imports and logging size metrics with cleanup.

import { importCost, cleanup, Lang } from 'import-cost-core'; import type { PackageInfo } from 'import-cost-core'; const fileName = 'src/index.ts'; const fileContents = ` import express from 'express'; import { readFile } from 'fs/promises'; const _ = require('lodash'); `; const emitter = importCost(fileName, fileContents, Lang.TYPESCRIPT); emitter.on('start', (packages: PackageInfo[]) => { console.log('Calculating sizes for', packages.length, 'packages...'); }); emitter.on('calculated', (pkg: PackageInfo) => { console.log(`${pkg.name}: minified=${(pkg.size / 1024).toFixed(2)} KB, gzip=${(pkg.gzip / 1024).toFixed(2)} KB, brotli=${(pkg.brotli / 1024).toFixed(2)} KB, tree-shakeable=${pkg.sideEffects === false}`); }); emitter.on('done', (packages: PackageInfo[]) => { console.log('All done!'); cleanup(); }); emitter.on('error', (err: Error) => { console.error('Error:', err.message); });
import-cost --version
Debug
Known issues
gotchaThe `importCost` function caches results based on file content. If you call it multiple times with the same content, you might get stale results. Dispose the emitter and create a new one for fresh calculations.
fix
Call `emitter.removeAllListeners()` before creating a new emitter for the same file.
affects: >=1.0
gotchaThe `PackageInfo` import is a TypeScript type, not a value. Attempting to use it at runtime will throw `ReferenceError: Cannot access 'PackageInfo' before initialization` in CommonJS or `PackageInfo is not defined` in ESM.
fix
Use `import type { PackageInfo } from 'import-cost-core'` to import only the type, or use `import { PackageInfo } from 'import-cost-core'` only if you need the runtime value (it does not exist).
affects: >=1.0
deprecatedThe default export `import importCost from 'import-cost-core'` is deprecated as of v3.0. Use named imports instead.
fix
Replace `import importCost from 'import-cost-core'` with `import { importCost } from 'import-cost-core'`.
affects: >=3.0
breakingIn v2.0, the function signature changed: `importCost(fileName, fileContents, language)` replaced the old `importCost(fileName, fileContents, language, config)`. The config object is now passed as the fourth argument, but the third argument is now required (previously optional).
fix
Update to v3.0+ which restored the optional config parameter or adjust your calls to include the language parameter.
affects: >=2.0 <3.0
gotchaThe `cleanup` function must be called to terminate background esbuild workers. If you don't call it, the Node.js process will not exit cleanly (it will hang).
fix
Call `cleanup()` when done, e.g., after the 'done' event or in a process 'exit' handler.
affects: >=1.0
Errors
Common errors & fixes
ReferenceError: Cannot access 'PackageInfo' before initialization
Importing `PackageInfo` as a value when it is only a TypeScript type.
fix
Use `import type { PackageInfo } from 'import-cost-core'` instead.
Error: Cannot find module 'import-cost-core'
Package not installed or not in node_modules.
fix
Run `npm install import-cost-core` in your project.
TypeError: importCost is not a function
Using default import in ESM when package expects named export, or misconfiguring bundler.
fix
Use `import { importCost } from 'import-cost-core'`.
Error: esbuild is not installed
Missing peer dependency 'esbuild'.
fix
Run `npm install esbuild` alongside import-cost-core.
Upgrade
Version history
5.5.0latest on npm
Audit
Dependencies
esbuildrequiredCore bundler used for size calculation
es-module-lexerrequiredFast ESM module analysis for tree-shaking detection
Agent activity
4 hits · last 30 days
node
4
Resources