Home > Enterprise >  I'm getting undefined packages even though I have them in my package.json and node_modules fold
I'm getting undefined packages even though I have them in my package.json and node_modules fold

Time:11-03

So I'm developing a web app using NestJS as its backend. Also I'm using docker to build all my backend. I'm having a problem that some installed packages logs as "undefined" and some other works properly.

For example in my CreateUser class I have

import bcrypt from 'bcrypt';

@Injectable()
export class CreateUser {
  constructor(
    @InjectRepository(User)
    private usersRepo: Repository<User>,
  ) {}

  async execute(input: CreateUserInput): Promise<User> {
    console.log('bcrypt', bcrypt);
    const user = this.usersRepo.create(input);
    user.email = input.email.trim().toLowerCase();
    user.password = await bcrypt.hash(input.password, 10);
    const newUser = await this.usersRepo.save(user);
    return newUser;
  }

When I execute this function it logs this

bcrypt undefined
data TypeError: Cannot read property 'hash' of undefined

But if I go to my node_modules I have the bcrypt module installed. And the same happens in my package.json

I think its a docker problem but I've gone into the container and inspected the node_modules folder and it also is installed in there. Here goes my docker file just in case

FROM node:12-alpine

ENV NODE_ENV development

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package.json package-lock.json /usr/src/app/

RUN npm install

COPY . .

ENTRYPOINT ["npm", "run", "start:dev"]

Any ideas of why im not being able to use this package?

CodePudding user response:

you should do like this

import {hash} from 'bcrypt'

and then

 user.password = await hash(input.password, 10);

to understand more please refer to :https://learnjsx.com/category/2/posts/es6-javaScript

  • Related