Sub topic 12
Checking the package with publint, Are the Types Wrong and a test consumer
This chapter assumes you have already built the @acme/slugify package from the earlier chapters. That means a tsconfig.json that emits dist/, a package.json with an exports map, and a files list. You should also be comfortable running npm and npx commands.
Why a passing build is not enough
tsc succeeding tells you only that your source compiles. It says nothing about whether a stranger can install the package and import it. Most packaging bugs appear only on the consumer's side:
- An
exportstarget points to a file that was never built. - The
typescondition sits afterdefault, so it is never matched. - A
.jsfile is treated as CommonJS when the code is ESM. - A declaration file imports a path that was not published.
Four checks catch these problems, from cheapest to most realistic:
npm pack --dry-runshows what would be shipped.publintlintspackage.jsonagainst the files on disk.- Are the Types Wrong simulates how TypeScript resolves your types under each resolution mode.
- A test consumer installs the real tarball in a separate project and compiles against it.
The full sequence
The commands run in this order from the package root:
npm run build
npm pack --dry-run
npx publint
npx @arethetypeswrong/cli --pack .
npm publishRun the test consumer between the Are the Types Wrong check and npm publish. The sections below take each step in turn.
Step 1: Read the file list from npm pack
npm pack --dry-run prints every file npm would put in the tarball, without creating it. Read the list. Do not skim it.
Check for these:
dist/index.jsanddist/index.d.tsare present.- Every file referenced by
exports,mainandtypesis present. - Declaration maps (
.d.ts.map) and source maps are present only if you meant to ship them. - No
src/, test files,.envfiles or editor config.
npm always includes package.json, the README and the license file, whatever files says. Everything else is controlled by files, so "files": ["dist"] keeps source and tests out.
Declaration maps point back to .ts source files. If src/ is not published, go-to-definition in a consumer's editor falls back to the .d.ts file. That is harmless. If you want consumers to jump into your original source, add src to files. Either choice is valid, but make it deliberately.
Step 2: Lint package.json with publint
publint reads package.json and checks it against the files on disk. It needs no configuration:
npx publintIt reports at three levels: errors, warnings and suggestions. Typical findings:
| Finding | What it means | Usual fix |
|---|---|---|
| An `exports` path does not exist | The target was not built, or the path has a typo | Rebuild, then correct the path |
| `types` condition after `default` | Conditions match in order, so TypeScript never sees the types entry | Move `types` first |
| File is ESM syntax but parsed as CJS | `type: "module"` is missing, or the extension is wrong | Set `type` or use `.mjs` / `.cjs` |
| `main` or `module` without `exports` | Modern resolvers lack an explicit public surface | Add an `exports` map |
| `files` omits a referenced file | The tarball will be broken | Add the directory to `files` |
publint checks structure, not types. It cannot tell whether your .d.ts files describe the right module format. That is the job of the next tool.
Step 3: Check type resolution with Are the Types Wrong
TypeScript finds your types differently depending on the consumer's moduleResolution setting. Are the Types Wrong (attw) packs your package, then resolves it under each mode and reports what happens:
npx @arethetypeswrong/cli --pack .The output is a table. The rows are entry points, such as @acme/slugify. The columns are resolution modes:
node10: older consumers, which usemainandtypes.node16 (from CJS): a NodeNext consumer compiling to CommonJS.node16 (from ESM): a NodeNext consumer compiling to ESM.bundler: consumers usingmoduleResolution: "bundler".
Each cell shows a result. The common failures are:
- No types: the entry resolved to JavaScript but no declaration file was found.
- Resolution failed: the mode could not resolve the entry at all.
- Masquerading as CJS: the
.d.tsfile is treated as CommonJS, but the JavaScript behind it is ESM. Types and runtime disagree about the module format. This is common when a dual package reuses one.d.tsfile for both formats. - Masquerading as ESM: the reverse.
- Internal resolution error: a relative import inside a
.d.tsfile fails, often because it lacks a.jsextension.
Not every red cell is a bug. An ESM-only package such as @acme/slugify resolves from a CommonJS consumer as "ESM (dynamic import only)". That is accurate for tools that cannot require ESM. Whether it is acceptable depends on who your consumers are. For an intentional ESM-only package, run attw with --profile esm-only. That profile ignores the CommonJS-related resolution results that cannot apply to such a package. Do not hide real problems with --ignore-rules unless you understand each rule you suppress.
Step 4: Build a test consumer from the tarball
The previous tools inspect the package. A test consumer uses it the way a real user would. Create a separate project outside your library repository, then install the tarball:
npm pack
mkdir ../slugify-consumer
cd ../slugify-consumer
npm init -y
npm pkg set type=module
npm install --save-dev typescript @types/node
npm install ../slugify/acme-slugify-1.0.0.tgzAdjust the relative path to where your library lives. A scoped package name such as @acme/slugify produces a tarball named acme-slugify-1.0.0.tgz.
Do not use npm link or a file: dependency on the project folder for this test. Both install from a directory or symlink, so files is not applied and unpublished files appear to exist. The tarball contains only what you would actually publish.
Give the consumer its own tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "out",
"strict": true,
"skipLibCheck": false
},
"include": ["index.ts"]
}Set skipLibCheck to false here. With true, TypeScript silently ignores errors inside your declaration files, which is exactly what you are trying to find.
Add index.ts. It exercises the public function, the public type, and one import that must be rejected:
import { slugify, type SlugOptions } from "@acme/slugify";
const options: SlugOptions = { separator: "_" };
console.log(slugify("Hello, World!", options));
// @ts-expect-error internal files are not listed in "exports"
import("@acme/slugify/dist/slugify.js");Compile and run it:
npx tsc
node out/index.jsYou should see hello_world. The compile step proves that the types resolve. The run step proves that the JavaScript loads under Node's ESM loader. The @ts-expect-error line proves that encapsulation works: if the import of an unlisted file ever compiles, the directive itself becomes an error, and you will know your exports map leaks internals.
Run the same consumer a second time with different settings:
- Change
moduleResolutionto"bundler"andmoduleto"ESNext"to check bundler-style consumers. - If you ship a dual package, add a CommonJS consumer (no
type: "module",module: "NodeNext") and userequire.
If your package is ESM-only, a require call works only on Node versions that support loading ESM synchronously (Node 22.12 and later, and 20.19 and later). On older versions it throws ERR_REQUIRE_ESM. State your supported Node range in the README so consumers know.
Automating the checks
Put the checks in a script so they run in CI and before every release:
{
"scripts": {
"build": "tsc",
"verify": "npm run build && publint && attw --pack .",
"prepublishOnly": "npm run verify"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.0",
"publint": "^0.3.0"
}
}The version ranges are examples. Install the current releases with npm install --save-dev publint @arethetypeswrong/cli, which writes the correct ranges for you. The test consumer is harder to script inside the library repository, so many teams keep it as a small CI job that packs the library and runs tsc in a fixture folder.
Common mistakes
- Fixing the tool output without understanding it. Look at the specific resolution mode that failed before changing the
exportsmap. - Running checks before building.
publintthen reports missing files that would exist afternpm run build. - Testing against the repository, not the tarball. Path mappings and workspace symlinks hide missing files.
- Leaving
skipLibCheckon in the consumer. Broken declaration files go unnoticed. - Treating warnings as noise. A warning about
typesordering looks minor and still breaks every TypeScript user.
Exercise
You run the checks on a dual ESM and CommonJS package. publint reports no problems, but Are the Types Wrong shows "Masquerading as CJS" for the node16 (from CJS) column on the root entry. What is the most likely cause, and how do you fix it?
Show answer
The require condition points to a declaration file that TypeScript treats as ESM (or the reverse). Typically one .d.ts file is reused for both formats, so the types describe a module format that does not match the JavaScript behind them. Give each format its own declaration file: .d.ts under the import condition and .d.cts under the require condition, each with its own nested types condition listed before default. Then rebuild, repack, and rerun attw. publint did not flag it because the paths exist and the ordering is valid. It does not check whether declaration formats match.
Exercise
Why should the test consumer install the .tgz file from npm pack instead of using npm link?
Show answer
npm link symlinks the project folder, so the consumer sees every file in the repository whether or not files includes it. A missing dist file or a leaked internal file goes unnoticed. The tarball contains only the files npm would publish, so it reproduces what real consumers get.