[英]I'm getting undefined packages even though I have them in my package.json and node_modules folder
So I'm developing a web app using NestJS as its backend.所以我正在开发一个使用 NestJS 作为后端的网络应用程序。 Also I'm using docker to build all my backend.此外,我正在使用 docker 来构建我的所有后端。 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例如在我的 CreateUser 类中,我有
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.但是如果我去我的 node_modules 我已经安装了 bcrypt 模块。 And the same happens in my package.json同样的情况也发生在我的 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.我认为这是一个 docker 问题,但我已经进入容器并检查了 node_modules 文件夹,它也安装在那里。 Here goes my docker file just in case这是我的 docker 文件以防万一
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?为什么我不能使用这个包的任何想法?
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了解更多请参考: https : //learnjsx.com/category/2/posts/es6-javaScript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.