cannot find module faker after npm install --save-dev
Asked Answered
E

8

31

I want to install all my modules locally so I am installing everything with the "--save-dev" switch which updates the package.json.

I am trying to include this module so I installed using this command:

npm install Faker --save-dev

My app structure is like this:

app controllers models node_modules Faker server.js

So Faker is in the right place but when I add this code in my server.js file:

var faker = require('./Faker');

I get the following error message:

Error: Cannot find module './Faker'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/paulcowan/projects/async-talk/server.js:23:13)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10) 

But this works:

var Faker = require('./node_modules/Faker');

I did not think that I would have to include the node_modules part.

Eldoneldora answered 18/2, 2014 at 21:54 Comment(0)
Q
3

To get your require to work, you need do:

var Faker = require('Faker');

Any package installed by npm is required by name. Only modules that are required locally need a path-like require. Your require(./Faker); means "require a module from the same directory as this file, called 'Faker'".

Quadrature answered 18/2, 2014 at 22:17 Comment(0)
P
61

After a yarn upgrade --latest I got this error for another reason: A 6.6.6 version has been published 13 days ago and it's totally empty (only one commit called endgame): https://www.npmjs.com/package/faker.

Is it a hack? Is it a joke? If yes, is it fun? Not sure...

To fix it simply rollback to the last version.

yarn add --dev [email protected]
# or if you use npm
npm install --save-dev [email protected]

Edit

Following the answer of akop below, the safest way seems to switch to faker-js

yarn remove faker
yarn add --dev @faker-js/faker

Then change your imports

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

There is also a type definition for Typescript on their doc but it worked without for me: github.com/faker-js/faker

Edit 2

I spoke with the author of the package who's a fun and crazy guy. He got mad because of an intellectual property theft so he kind of destroyed his open-sourced work.

He now develops a 2000s style virtual desktop for fun buddypond.com

Pauiie answered 18/1, 2022 at 10:17 Comment(0)
S
22

I want to mention everyone why this problem happened.
The owner of faker deleted it from GitHub at v6.6.6
You can see that here:
https://github.com/marak/Faker.js/

So it is up to the community to complete it
The new repository:
https://github.com/faker-js/faker

This is the official, stable fork of Faker.

Installation

Please replace your faker dependency with @faker-js/faker.

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

Node.js

const { faker } = require('@faker-js/faker');
Sleeper answered 2/2, 2022 at 15:6 Comment(0)
W
15

Faker (and colors) was destroyed by the author. The reason is not totally clear.

You can use the version before the endgame-commit (5.5.3), as it already Jean Claveau described.
For the future, you should switch to the new community version, it is called fake-js. Have a look here: Github of new faker-js
There is also a faq, which also explains what happens to the old version.

Warhol answered 18/1, 2022 at 10:41 Comment(0)
W
6

Typescript users coming by, remember to install @types/faker. Then, import faker from 'faker'; works without errors.

Warmonger answered 29/5, 2020 at 18:52 Comment(2)
For some people, this will be import * as faker from 'faker';Silicify
does that need to be installed as --save-dev or a regular install?Alodium
S
5

The latest version of faker is v7.3.0 .

You should import faker in this way:

import { faker } from '@faker-js/faker';

This one below doesn't work:

import faker from 'faker';

Summerlin answered 30/6, 2022 at 3:15 Comment(0)
C
5

Genuine solution : Roll back to the previous version of the faker i.e. install the 5.5.3 or any lower version. It will work fine.

npm install faker @5.5.3

or

yarn install faker @5.5.3

NOTE : Faker has been changed to fakerjs so if you want to use latest version of faker please install it from fakerjs using the following command :~

npm i @faker-js/faker

Coastline answered 16/9, 2022 at 11:1 Comment(0)
Q
3

To get your require to work, you need do:

var Faker = require('Faker');

Any package installed by npm is required by name. Only modules that are required locally need a path-like require. Your require(./Faker); means "require a module from the same directory as this file, called 'Faker'".

Quadrature answered 18/2, 2014 at 22:17 Comment(0)
O
1

Remove the ./. You're telling Node to look for the module in the current directory.

var faker = require('Faker');
Oke answered 18/2, 2014 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.