question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Package.json star and directory exports not working

See original GitHub issue

Describe the bug

The newly introduced package.json exports field explicitly defines the exports map for a library.

  • When describing exports as a directory map, it should forward dependency resolution to the classic resolution mechanism, but it does not.
    • Given the export "./mydir/": { "import": "./build/mydir/" }
    • Vite.js should be able to resolve package/mydir/nested/path to package/build/mydir/nested/path.js or package/build/mydir/nested/path/index.js but it fails.
  • WebPack also introduced star exports, that allow you to resolve nested exports to specific file types.
    • Given the export: "./mydir/*": { "import": "./build/mydir/*/*.js" }
    • Vite.js should be able to resolve /mydir/nested/path to package/build/mydir/nested/path.js but it fails.

Reproduction

  1. npm init @vitejs/app
  2. npm install @inkline/icons
  3. Edit node_modules/@inkline/icons/package.json in place and add the following:
  "exports": {
    ".": {
      "import": "./build/index.js"
    },
    "./packs": {
      "import": "./build/packs/index.js"
    },
    "./packs/": {
      "import": "./build/packs/"
    },
    "./helpers/*": {
      "import": "./build/helpers/*.js"
    }
  },
  1. Edit src/main.ts and add the following:
import { toCamelCase } from '@inkline/icons/helpers';
import { fasHome } from '@inkline/icons/packs/fontawesome';
import { inkCheck } from '@inkline/icons/packs/inkline';

console.log(toCamelCase, fasHome, inkCheck);
  1. npm run dev

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:


  System:
    OS: macOS 10.15.4
    CPU: (4) x64 Intel(R) Core(TM) i5-6600 CPU @ 3.30GHz
    Memory: 53.07 MB / 32.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    npm: 6.14.11 - ~/.nvm/versions/node/v14.16.0/bin/npm
  Browsers:
    Chrome: 90.0.4430.212
    Firefox: 75.0
    Safari: 13.1
  npmPackages:
    @vitejs/plugin-vue: ^1.2.2 => 1.2.2 
    vite: ^2.3.0 => 2.3.2 

Used package manager: npm

Logs

For directory exports:

> vite-project@0.0.0 dev /Users/alexgrozav/Workspace/vite-project
> vite

error when starting dev server:
Error: The following dependencies are imported but could not be resolved:

  @inkline/icons/packs/fontawesome (imported by /Users/alexgrozav/Workspace/vite-project/src/main.ts)

Are they installed?
    at optimizeDeps (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:64623:15)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async runOptimize (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:68436:48)
    at async Server.httpServer.listen (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:68450:17)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! vite-project@0.0.0 dev: `vite`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the vite-project@0.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/alexgrozav/.npm/_logs/2021-05-14T06_26_12_449Z-debug.log

For star exports:

> vite-project@0.0.0 dev /Users/alexgrozav/Workspace/vite-project
> vite

 > node_modules/vite/dist/node/chunks/dep-efbff8d5.js:31957:7: error: [plugin: vite:dep-scan] Missing "./helpers" export in "@inkline/icons" package
    31957 │   throw new Error(
          ╵         ^
    at bail (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:31957:8)
    at resolve$1 (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:32033:10)
    at resolveExports (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:32462:12)
    at resolveDeepImport (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:32479:26)
    at tryNodeResolve (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:32268:11)
    at Context.resolveId (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:32152:28)
    at Object.resolveId (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:44098:55)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async resolve (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:44294:26)
    at async /Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:44402:34

   node_modules/vite/dist/node/chunks/dep-efbff8d5.js:44392:18: note: This error came from the "onResolve" callback registered here
    44392 │             build.onResolve({
          ╵                   ~~~~~~~~~
    at setup (/Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:44392:19)
    at handlePlugins (/Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:733:23)
    at Object.buildOrServe (/Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:1018:7)
    at /Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:1742:17
    at new Promise (<anonymous>)
    at Object.build (/Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:1741:14)
    at Object.build (/Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:1617:51)
    at /Users/alexgrozav/Workspace/vite-project/node_modules/vite/dist/node/chunks/dep-efbff8d5.js:44255:54
    at Array.map (<anonymous>)

   src/main.ts:2:28: note: The plugin "vite:dep-scan" was triggered by this import
        2 │ import { toCamelCase } from '@inkline/icons/helpers';
          ╵                             ~~~~~~~~~~~~~~~~~~~~~~~~

error when starting dev server:
Error: Build failed with 1 error:
node_modules/vite/dist/node/chunks/dep-efbff8d5.js:31957:7: error: [plugin: vite:dep-scan] Missing "./helpers" export in "@inkline/icons" package
    at failureErrorWithLog (/Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:1443:15)
    at /Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:1125:28
    at runOnEndCallbacks (/Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:915:63)
    at buildResponseToResult (/Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:1123:7)
    at /Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:1230:14
    at /Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:606:9
    at handleIncomingPacket (/Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:703:9)
    at Socket.readFromStdout (/Users/alexgrozav/Workspace/vite-project/node_modules/esbuild/lib/main.js:573:7)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! vite-project@0.0.0 dev: `vite`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the vite-project@0.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/alexgrozav/.npm/_logs/2021-05-14T06_25_10_219Z-debug.log


Before submitting the issue, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
  • Provide a description in this issue that describes the bug.
  • Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sodateacommented, Jun 24, 2021

Subpath folder mappings are deprecated in Node.js https://nodejs.org/api/packages.html#packages_subpath_patterns so I don’t think we should support it.

But yeah, subpath patterns should be supported.

1reaction
ignatiusmbcommented, Jun 3, 2021

I can confirm, even without exports map and just default directory traversal, modules exported with the following resolves to undefined.

// geometry/index.js
export * as shapes from './shapes';

// geometry/shapes/index.js
export function circle() {}
export function rect() {}
export function triangle() {}


// assume module is installed with npm
import { shapes } from 'module/geometry';
console.log(shapes); // undefined

import * as shapes from 'module/geometry/shapes';
console.log(shapes); // works -> { circle: [Getter], rect: [Getter], triangle: [Getter] }

Directory structure for example above

module
│
└───geometry
      ├──shapes
      │     └──index.js
      │
      └──index.js

Edit: This will be a problem when library authors only exposes ./geometry in the exports map and essentially locking the imports. This means users can’t directly import from module/geometry/shapes because it’s not defined in the exports.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I use "exports" in package.json today for nested ...
I've been looking to use nested folders for a design system package we created at Pipefy, and after deep research, I found how...
Read more >
npm-link
json , not from the directory name. The package name can be optionally prefixed with a scope. See scope . The scope must...
Read more >
Requiring modules in Node.js: Everything you need to know
Requiring a module in Node isn't that complicated of a concept. const config = require('/path/to/file');. The main object exported by the ...
Read more >
API - ESBuild
Tsconfig; Working directory. #Simple options. #Alias. Supported by: Build. This feature lets you substitute one package for another when bundling ...
Read more >
What does it take to support Node.js ESM? – The Guild
This post is intended to work as a guide to support both CommonJS and ... For the reason just mentioned, the standard package.json#exports...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found