Open
Description
Overview
We currently define an exports
field in our package.json
, with additional files (such as compiled tokens) available via other fields.
But according to the Node docs, this prevents other files from being imported:
When the "exports" field is defined, all subpaths of the package are encapsulated and no longer available to importers. For example, require('pkg/subpath.js') throws an ERR_PACKAGE_PATH_NOT_EXPORTED error.
I've experienced this in a project where I wanted to import our design tokens.
Steps to reproduce
You can follow these steps to create a barebone test case project:
- Create a directory:
mkdir cloudfour-export-issue
- Enter that directory:
cd cloudfour-export-issue
- Initialize a new npm project:
npm init -y
- Install our patterns:
npm i -D @cloudfour/patterns
- Create a script that attempts to require tokens:
printf "const tokens = require(\"@cloudfour/patterns/src/compiled/tokens/json/tokens.json\");" > index.js
- Run script:
node index
Here is an example of the error you will likely see:
node:internal/modules/cjs/loader:499
throw e;
^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './src/compiled/tokens/json/tokens.json' is not defined by "exports" in /PATH/TO/cloudfour-export-issue/node_modules/@cloudfour/patterns/package.json
at new NodeError (node:internal/errors:387:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:464:9)
at packageExportsResolve (node:internal/modules/esm/resolve:748:3)
at resolveExports (node:internal/modules/cjs/loader:493:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:533:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:942:27)
at Function.Module._load (node:internal/modules/cjs/loader:804:27)
at Module.require (node:internal/modules/cjs/loader:1028:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/tylersticka/Repos/exports-test/index.js:1:16) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}