uuid: package.json is not defined by "exports"

Describe the bug

‘./package.json’ is not defined by “exports” when upgrading from v7.0.3 to v8.0.0

To Reproduce

Steps to reproduce the behavior:

  1. upgrading from v7.0.3 to v8.0.0 and running npm build in a Svelte project
  2. See error

Expected behavior

A rollup bundle without an error as shown below.

Runtime

  • OS: macOS
  • Runtime: Node.js
  • Runtime Version: 14.1.0

Additional context

This is the command line output after upgrading from v7.0.3 to v8.0.0 in a Svelte project

> Package subpath './package.json' is not defined by "exports" in /Users/sa/0/edm/node_modules/uuid/package.json
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in /Users/sa/0/edm/node_modules/uuid/package.json
    at applyExports (internal/modules/cjs/loader.js:491:9)
    at resolveExports (internal/modules/cjs/loader.js:507:23)
    at Function.Module._findPath (internal/modules/cjs/loader.js:635:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1007:27)
    at Function.requireRelative.resolve (/Users/sa/0/edm/node_modules/require-relative/index.js:30:17)
    at tryResolve (/Users/sa/0/edm/node_modules/rollup-plugin-svelte/index.js:50:19)
    at Object.resolveId (/Users/sa/0/edm/node_modules/rollup-plugin-svelte/index.js:177:21)
    at /Users/sa/0/edm/node_modules/rollup/dist/shared/rollup.js:18217:25

I found some additional information in a SO post that might very well be related to the issue I’m seeing https://stackoverflow.com/questions/61621828/getting-strange-error-using-uuid-npm-module-what-am-i-doing-wrong/61730597#61730597

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 27 (13 by maintainers)

Commits related to this issue

Most upvoted comments

I fixed it by running npm ci.

In general, I think it’s a good idea for packages to explicitly add "./package.json": "./package.json" to their "exports" field.

However, unless the ecosystem is consistently willing to do this, tooling will have to determine a different mechanism for accessing package.jsons - something like findPackageJSONFrom(path.dirname(require.resolve('pkg'))).

Hi @ctavan ,

I am seeing the same error using the latest version 8.1.0 but for the v4 exports:

Package subpath './v4' is not defined by "exports" in /Users/path-to-project/nested/in/node-modules-folder/node_modules/uuid/package.json

$ node -v
v14.4.0

Just release uuid@8.1.0 which now exports package.json, so the problem reported here should be gone for now.

@alayor thanks , save my ass

It is not only a Svelte Rollup problem.

I upgraded to v8.0.0 in my React Native project and encountered the exact same problem.

To rephrase @ljharb’s suggestion (assuming Node.js won’t change the current behavior, see https://github.com/nodejs/node/issues/33460):

  1. Packages that rely on meta information for other tools from their package.json must export it.
  2. Tools must be able to handle dependencies which don’t export package.json gracefully (i.e. without crashing).
    • Tools which make use of meta information from package.json could warn if packages don’t export package.json in order to help package maintainers decide if they need to export it or not.

For the time being the warning can simply be ignored for uuid, the functionality is not affected!

I think it might make more sense to actually fix this in react-native instead of adding this workaround to every single module on npm. If you agree, feel free to give your 👍 for https://github.com/react-native-community/cli/issues/1168

I’m using uuid inside my server.js like so

import { v4 as uuidv4 } from 'uuid'

... more code...


const setNonceMiddleware = ( request, response, next ) => {
  try {
    response.locals.nonce = uuidv4()
    next()
  } catch ( error ) {
    console.error ( 'error from setNonceMiddleware: ', error )
  }
}

I don’t use uuid anywhere else in my app, just the snippet above. I’ll also provide a gist of my rollup config that might help, it’s a Sapper (Svelte) project with a client and a server as well as a serviceworker section: https://gist.github.com/7b56754df52b5b26b1db3f2873cee726

Thanks for the pointers. The problem is actually with npm/lerna where one package didn’t get it’s (newer) version of uuid installed locally and was using a (probably) deduped one in the parent node_modules folder coming from an older package. Quite a specific use case, nothing wrong with uuid 👍

This is a different issue, support for deep imports has been deprecated in 7.0.0 and removed in 8.0.0: https://github.com/uuidjs/uuid#deep-requires-no-longer-supported