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

Viewed 11269

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?

6 Answers

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.

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 },
]; 

Install the package using:

npm install uuid
  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(),
    ....
}

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'

This worked for me

import * as uuid from 'uuid'


uuid.v4()
Related