1

At first, I install faker using the command npm i faker

Now i am trying to run my npm server up

npm run start.

But i am getting the following error. Can some one please help me here and thanks in advance.

**Compiled with problems:X

ERROR in ./src/context/Context.js 5:0-26

Module not found: Error: Can't resolve 'faker' in shoppingcartapplication\src\context**

import React, { createContext } from 'react';
import faker from "faker";

const Cart = createContext();
faker.seed(20);

const Context = ({ children }) => {
    
    const products = [...Array(20)].map(() => ({
        id: faker.datatype.uuid(),
        name: faker.commerce.productName(),
        price: faker.commerce.price(),
        image: faker.random.image(),
        inStock: faker.random.arrayElement([0, 3, 5, 6, 7]),
        fastDelivery: faker.datatype.boolean(),
        ratings: faker.random.arrayElement([1, 2, 3, 4, 5]),
      }));

      console.log(products);

  return (
    (
        <Cart.Provider >
          {children}
        </Cart.Provider>
      )
  )
}

export default Context;

sruthi
  • 35
  • 1
  • 5

3 Answers3

0

faker is deprecated first remove your old package

npm uninstall faker

then get a new package

npm install @faker-js/faker --save-dev

and import it like this

import * as faker from 'faker';

import * as faker from '@faker-js/faker';

check here for more info

Mohammad
  • 722
  • 1
  • 8
  • 17
0
npm install --save-dev faker@5.5.3

this will solve the issue

Shah Vipul
  • 625
  • 7
  • 11
0
import { faker } from '@faker-js/faker';

//single random user profile generator
function randomProfile() {
    return {
        userId: faker.datatype.uuid(),
        username: faker.internet.userName(),
        email: faker.internet.email(),
        avatar: faker.image.avatar(),
        password: faker.internet.password(),
        birthdate: faker.date.birthdate(),
        registeredAt: faker.date.past(),
    }
}

//define a method to generate users up to 'max_size' amount
const profile = function (max_size) {
    const  users = [];
    for (let index = 0; index < max_size; index++) {
        users.push(randomProfile());
    }
    return users;
};

// actually generate 10 random user profiles & load them in 'users_group' variable
const users_group = profile(10);


const Home = () => {
  return (
    <View style={{flex: 1, backgroundColor: '#fff'}}>
        <StatusBar hidden/>
         {/* out of the 10 user profile generated use the avatar(Image) of the first user*/}
        <Image source={users_group[1].avatar} />

         {/* out of the 10 user profile generated use the second avatar(Image) of the first user*/}
        <Image source={users_group[2].avatar} />
        <Text>Hello{profile.avatar}</Text>
    </View>
  )
}
ITC ADU
  • 46
  • 2