Kaumadie Kariyawasam
Kaumadie Kariyawasam

Reputation: 1456

Can't resolve 'uuid/v4' in react-native

when I running my react-native project it gives following error:

Failed to compile.

Module not found: Can't resolve 'uuid/v4' in 'F:\App 2nd\chat2\SecreteChat\node_modules\expo-constants\build'

How I solve this?

Upvotes: 8

Views: 23233

Answers (8)

Penny Liu
Penny Liu

Reputation: 17428

Instead of using the uuid library, you can use JavaScript's built-in crypto API with randomUUID().

console.log(crypto.randomUUID())

Upvotes: 0

Shree
Shree

Reputation: 111

Note - I am using npm and no need to change any node module files.

This worked for me -

replace

import uuidv4 from "uuid/v4" 

with

import {v4 as uuidv4} from "uuid"

Hope this helps :)

Upvotes: 1

Kapilrc
Kapilrc

Reputation: 1538

  1. Install uuid module
npm install uuid
  1. Create a UUID (ES6 module syntax)
import { v4 as uuidv4 } from 'uuid';
uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

... or using CommonJS syntax:

const { v4: uuidv4 } = require('uuid');
uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'

then you can use it to set id, like

const person = {
    id: uuidv4(),
    ....
}

Upvotes: 0

Mide Roberts
Mide Roberts

Reputation: 31

Here are the steps i followed to resolve it:

  • install uuid (I assume you've done this already).

    npm install uuid

  • Then import uuidv4 from uuid library

    const { v4: uuidv4 } = require('uuid');

  • You can then go ahead to use it. eg:

const expenseList = [
   { id: uuidv4(), expenseItem: "rent", amount: 1000 },
   { id: uuidv4(), expenseItem: "car note", amount: 2000 },
   { id: uuidv4(), expenseItem: "mortgage", amount: 3000 },
]; 

Upvotes: 3

akili Sosa
akili Sosa

Reputation: 209

You have to update the node_modules/expo-constants/exponentConstants.web.js

-- side note I was always run the expo web so I don't know if there is a Expoconstants.Android... or expoConstants.ios... that you will have to update as well. I also have been using yarn add ____ for most of my dependencies so if you were doing something different let us know. the error line above the error should tell you where it is originating from mine says its here:

Y:/Serviced/inService/node_modules/expo-constants/build/ExponentConstants.web.js

navigate to that file, and look around line number 3 in node_modules/expo-constants/exponentConstants.web.js

import uuidv4 from 'uuid/v4

and change it to:

import {v4 as uuidv4} from 'uuid';

This error happens because of the file structure in node_modules/uuid, if you look there is no longer a uuidv4 to import and instead they export a v4. You could change all the places where the developers wrote uuidv4 to v4 but using the { this as that } syntax you don't have to rewrite a bunch of code.

Upvotes: 14

Rupesh Chaudhari
Rupesh Chaudhari

Reputation: 308

Install the package using:

npm install uuid

Upvotes: 0

JAY
JAY

Reputation: 9

This worked for me

import * as uuid from 'uuid'


uuid.v4()

Upvotes: 0

Omar
Omar

Reputation: 1

I fixed this problem ... just you should'nt put this :

import uuidv4 from 'uuid/v4'

but you should put

import uuid from 'uuid/package.json'

Upvotes: -2

Related Questions