Full answer
Two maps live in package.json:
- exports: the public entry points of a package, used by other packages.
- imports: private aliases used inside the package. Every key must start with #. Nothing outside the package can use them.
Features
- Simple mapping: "#config": "./src/config.js".
- Conditions: "#db": { "node": "./src/db-node.js", "default": "./src/db-fallback.js" }. Node tests the keys in the order written and takes the first condition it supports. default should be last.
- Standard conditions: import, require, node, default, module-sync, and others such as types for TypeScript. Custom conditions can be enabled with node --conditions=development, for example for a dev-only file.
- Subpath patterns: "#utils/": "./src/utils/.js" maps #utils/math to ./src/utils/math.js. A pattern can also be used in exports.
- A target can be another package: "#fetch": "undici" (a bare package name, which keeps one place to swap implementations).
- Self-reference: a package with an exports field can import itself by its own name, for example import 'my-package/feature'.
Why use imports
- No fragile relative paths such as ../../../lib.
- Mocking and environment switching: map a name to a different file with a condition, without changing the importing code.
- Works in CommonJS and ESM, with no build step or bundler.
Gotchas
- Target paths must start with ./ and stay inside the package.
- A missing key throws ERR_PACKAGE_IMPORT_NOT_DEFINED.
- With exports present, deep imports to unlisted files fail with ERR_PACKAGE_PATH_NOT_EXPORTED.
- TypeScript needs moduleResolution node16, nodenext or bundler to understand imports.
javascript
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { spawnSync } = require('node:child_process');
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'imports-'));
const write = (file, text) => { fs.mkdirSync(path.dirname(path.join(dir, file)), { recursive: true }); fs.writeFileSync(path.join(dir, file), text); };
write('package.json', JSON.stringify({
name: 'school-app',
type: 'module',
exports: { '.': './src/index.js', './feature': './src/feature.js' },
imports: {
'#db': { development: './src/db-memory.js', node: './src/db-node.js', default: './src/db-other.js' },
'#utils/*': './src/utils/*.js',
},
}, null, 2));
write('src/db-node.js', "export const name = 'node database';");
write('src/db-memory.js', "export const name = 'in-memory database';");
write('src/utils/math.js', 'export const double = (n) => n * 2;');
write('src/feature.js', "export const feature = 'feature module';");
write('main.js', `
import { name } from '#db';
import { double } from '#utils/math';
import { feature } from 'school-app/feature';
console.log('#db ->', name);
console.log('#utils/math ->', double(21));
console.log('self reference ->', feature);
for (const spec of ['#missing', 'school-app/src/utils/math.js']) {
try { await import(spec); } catch (err) { console.log(spec, '->', err.code); }
}
`);
for (const args of [[], ['--conditions=development']]) {
const run = spawnSync(process.execPath, [...args, 'main.js'], { cwd: dir, encoding: 'utf8' });
console.log('--- node', args.join(' ') || '(no flags)');
console.log(run.stdout.trim());
}
fs.rmSync(dir, { recursive: true });A temporary package defines imports with a condition, a pattern and a self-reference. It runs twice, with and without the development condition. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): --- node (no flags) #db -> node database #utils/math -> 42 self reference -> feature module #missing -> ERR_PACKAGE_IMPORT_NOT_DEFINED school-app/src/utils/math.js -> ERR_PACKAGE_PATH_NOT_EXPORTED --- node --conditions=development #db -> in-memory database #utils/math -> 42 self reference -> feature module #missing -> ERR_PACKAGE_IMPORT_NOT_DEFINED school-app/src/utils/math.js -> ERR_PACKAGE_PATH_NOT_EXPORTED
Key points
- imports keys start with # and are private to the package.
- Conditions are tried in order, so put default last.
- Subpath patterns use * in the key and the target.
- --conditions adds custom conditions such as development.
- exports defines the public API, imports defines private aliases.
Common mistakes
- Writing an imports key without the # prefix.
- Putting default before the specific conditions, so they never match.
- Pointing a target outside the package folder.
- Forgetting that exports blocks deep imports.
- Expecting TypeScript to resolve imports without the right moduleResolution.
Follow-ups
- How does the imports map differ from path aliases in tsconfig?
- How do you provide different files for import and require in exports?
- How can imports help with mocking in tests?
Tips
- Say 'private aliases' for imports and 'public API' for exports.
- Mention condition order.
- Give the benefit: no ../../ paths and no bundler.