Search code examples
javascriptnode.jsexpressnode-modulesuuid

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './v4' is not defined by "exports" in C:\User\mern-app\BACKEND\node_modules\uuid\package.json


Node version is :
enter image description here

uuid version is : 9.0.1

When I`m running the node server encountering the following error : enter image description here

How am I using the uuid lib is :
Importing it as :

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

utilizing it as:

id: uuid()

Solution

  • By keeping in mind about version compatibility of node & uuid.

    We are trying to import the specific version of the uuid. This error is bcz of the way uuidv4(version 4) is being imported & used.

    The uuid package updated its structure from version 8.0.0 & the way to import specific UUID versions like v4 has been modified.

    In order to solve this error, follow the steps :

    1. Update the import statement : const { v4: uuidv4 } = require('uuid');
    2. Utilize it as :
    const { v4: uuidv4 } = require('uuid'); // this is how we imported it.
    
    const uniqueId = uuidv4();
    console.log(uniqueId);
    

    Doing the above 2 step will solve this error.

    There is a bit of difference in how we use it with ES6 module & CommonJS

    For in-depth usage & explanation, please refer to the documentation here.