Node Exports Info
node-exports-info is a utility library providing programmatic access to detailed information about Node.js's `exports` field support across different Node.js versions. It helps developers understand which `exports` features, such as conditions, patterns, and pattern trailers, are available in specific Node.js environments. The library categorizes Node.js versions into distinct groups based on their `exports` implementation, including 'pre-exports', 'broken', 'experimental', 'conditions', and various pattern-related categories. This is crucial for authors of libraries and applications needing to ensure correct module resolution and interoperability across a wide range of Node.js versions, particularly given the evolving landscape of ESM and CJS module systems. Currently at version 1.6.0, it is actively maintained with updates likely correlating to significant changes in Node.js's module resolution behavior. Its primary differentiator is providing a comprehensive and version-specific mapping of `exports` capabilities, simplifying compatibility concerns for complex `package.json` `exports` configurations.
Common errors
-
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'node-exports-info' in ...
cause Attempting a named import from the main package path or an incorrect subpath, but individual functions are exposed as default exports via specific subpaths.fixChange the import statement to target the specific subpath for the desired function using a default import, e.g., `import getCategory from 'node-exports-info/getCategory';` -
TypeError: Cannot read properties of undefined (reading 'flags') OR 'getCategoryInfo is not a function'
cause Incorrectly importing a function or attempting to use a function that was not correctly imported as a default export from its specific subpath.fixVerify that the import statement correctly uses the `default` import syntax for the specific subpath of the function you intend to use. For example, `import getCategoryInfo from 'node-exports-info/getCategoryInfo';`
Warnings
- gotcha The library reflects the highly nuanced and sometimes inconsistent behavior of Node.js `exports` field across different versions. Developers should carefully interpret the category information and flags, especially when targeting older or experimental Node.js releases, as the `exports` specification evolved considerably.
- gotcha When importing specific utility functions, ensure you use the exact subpath import as documented (e.g., `import getCategory from 'node-exports-info/getCategory'`). Direct named imports from the main package or incorrect subpaths will result in `Module not found` or `undefined` errors.
Install
-
npm install node-exports-info -
yarn add node-exports-info -
pnpm add node-exports-info
Imports
- getCategory
import { getCategory } from 'node-exports-info'; // Incorrect: not a named export from the root packageimport getCategory from 'node-exports-info/getCategory';
- getCategoriesForRange
const getCategoriesForRange = require('node-exports-info').getCategoriesForRange; // Incorrect: attempting to access as a named CommonJS export from rootimport getCategoriesForRange from 'node-exports-info/getCategoriesForRange';
- getCategoryInfo
import { getCategoryInfo } from 'node-exports-info/getCategoryInfo'; // Incorrect: it's a default export, not a named one from the subpathimport getCategoryInfo from 'node-exports-info/getCategoryInfo';
Quickstart
import getCategory from 'node-exports-info/getCategory';
import getCategoryInfo from 'node-exports-info/getCategoryInfo';
import getRangePairs from 'node-exports-info/getRangePairs';
// Get the exports category for the current Node.js version
const currentCategory = getCategory();
console.log(`Current Node.js exports category: ${currentCategory}`);
// Get detailed info for a specific category, e.g., 'patterns'
const patternInfo = getCategoryInfo('patterns');
console.log('\nInfo for 'patterns' category:');
console.log(JSON.stringify(patternInfo, null, 2));
// Get info for the current category, specifically for 'require' module system
const currentCategoryRequireInfo = getCategoryInfo(currentCategory, 'require');
console.log(`\nCurrent category ('${currentCategory}') flags for 'require':`);
console.log(JSON.stringify(currentCategoryRequireInfo.flags, null, 2));
// List all categories with their associated semver ranges
console.log('\nAll exports categories and their Node.js version ranges:');
getRangePairs().forEach(([range, category]) => {
console.log(`- ${category}: ${range}`);
});