Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './v4' is not defined by "exports"

Tags:

node.js

I got this error when using uuidv4.

Failure: Package subpath './v4' is not defined by "exports" in C:\Users\mycomp\Desktop\Programming\Javascript\Serverless\Serverless Framework\node_modules\uuid\package.json
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './v4' is not defined by "exports" in C:\Users\mycomp\Desktop\Programming\Javascript\Serverless\Serverless Framework\node_modules\uuid\package.json

I already installed uuid and require it in my code

const uuidv4 = require('uuid/v4');

Here's the package.json

"dependencies": {
  "aws-sdk": "^2.702.0",
  "moment": "^2.27.0",
  "serverless-offline": "^6.4.0",
  "underscore": "^1.10.2",
  "uuid": "^8.1.0"
}
like image 635
The One Avatar asked Jun 24 '20 07:06

The One


3 Answers

ECMAScript Module syntax:

import { v4 as uuidv4 } from 'uuid';
uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

CommonJS syntax:

const { v4: uuidv4 } = require('uuid');
uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
like image 171
Ajith P Mohan Avatar answered Nov 16 '22 23:11

Ajith P Mohan


Another option

const uuid = require('uuid');
uuid.v4(); // "c438f870-f2b7-4b2c-a1c3-83bd88bb1d79"
like image 24
Miguel Trejo Avatar answered Nov 16 '22 23:11

Miguel Trejo


We had the same error with v1 uuid module (v8.3.2).

Solved this, adding following entry to exports section of installed uuid package.json(inside your node_modules):

"./v1": "./dist/v1.js"

Full export section of my projects' node_modules/uuid/package.json:

  "exports": {
    ".": {
      "node": {
        "module": "./dist/esm-node/index.js",
        "require": "./dist/index.js",
        "import": "./wrapper.mjs"
      },
      "default": "./dist/esm-browser/index.js"
    },
    "./package.json": "./package.json",
    "./v1": "./dist/v1.js"
  },

The problem remaining i now need to keep this modification across dist installs... :/

This could be fixed with a patch on uuid source itself?

EDIT: Didn't require the module in our own source. It is a dependency of jest (via some jest reporting sub-pkg).

EDIT: Alternatively, rolling back to uuid dep to v7.0.3 may fix this issue too, see comment below.

like image 40
Tchakabam Avatar answered Nov 17 '22 01:11

Tchakabam