How to use bcryptjs with ES6 NodeJS
Asked Answered
P

4

5

I'm trying to use bcryptjs package in my React project. I'm getting following error:

Could not find a declaration file for module 'bcryptjs'. ‘…/node_modules/bcryptjs/index.js' 
implicitly has an 'any' type.
Try `npm i --save-dev @types/bcryptjs` if it exists or add a new declaration (.d.ts)
file containing `declare module 'bcryptjs';`

Running npm i --save-dev @types/bcryptjs makes import error to go away but when I try to use variables from the package, they are not found.

NodeJS version is node v16.17.0 and TS is use as well.

How can I fix it? Thanks

Patio answered 9/11, 2022 at 14:43 Comment(1)
maybe this helps you?: #49443796Heraclitean
T
4

Try using below option:

npm i bcryptjs @types/bcryptjs

and then, add the below line

import { hash, compare } from 'bcryptjs';

Make sure that "allowSyntheticDefaultImports" is set to "true" in your tsconfig.json file

Tetrastichous answered 16/5, 2023 at 8:21 Comment(0)
G
2

Try running the command npm i --save-dev @types/bcryptjs or yarn add --dev @types/bcryptjs if you're using yarn to get the type definitions for this module.

Globuliferous answered 16/5, 2023 at 7:4 Comment(0)
C
0

I had a similar problem. To fix it, I first did:

yarn add bcryptjs @types/bcryptjs

And then removed the curly braces, when importing bcrypt. So, instead of:

import { bcrypt } from 'bcryptjs';

I did:

import bcrypt from 'bcryptjs';
Campania answered 3/11, 2023 at 2:34 Comment(0)
D
0

Running

npm i --save-dev @types/bcryptjs

makes import error to go away..

#RUN IN ROOT FOLDER FIRST

Defluxion answered 6/1 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.