0
I was setting up an app when I came across the following mistake:
The interesting thing is that I use the same function in another application and it works normally. I wonder if you have ever come across this mistake and how you managed to correct it.
Code:
import { getRepository } from 'typeorm';
import { join } from 'path';
import fs from 'fs';
import AppError from '../../errors/AppError';
import User from '../../models/User';
import uploadConfig from '../../config/upload';
interface Request {
user_id: string;
avatarFilename: string;
}
class UpdateUserAvatarService {
public async execute({ user_id, avatarFilename }: Request): Promise<User> {
const userRepository = getRepository(User);
const user = await userRepository.findOne(user_id);
if (!user) {
throw new AppError('Only authenticated users can change avatar', 401);
}
if (user.avatar) {
const userAvatarFilePath = join(uploadConfig.directory, user.avatar);
const userAvatarFileExists = await fs.promises.stat(userAvatarFilePath);
}
}
The version of Node I’m using is the v12.18.1
Saul, try to post the full code, not an image, so maybe we can help you
– Patrick Perdigão
Ready @Patrickperdigão
– Saulo Lins
Have you tried
import * as fs from 'fs';
?– LeAndrade
@Rafaeltavares Here to documentation with the President
– Saulo Lins
@Leandrade I tried yes, no fuciona
– Saulo Lins
@Saulolins gave a search, try to import from
fs/promises
. Apparently, see Pull Request, depending on the Node version you need to import from a different place– Rafael Tavares
@Rafaeltavares my version of Node is v12.18.1, the interesting thing is that I use this same call in another project, in this same pc and is not affected by this error, but I will try and bring the feedback
– Saulo Lins
@Rafaeltavares the error that returned was :
Não é possível localizar o módulo 'fs/promises' ou suas declarações de tipo correspondentes.
– Saulo Lins
Using require worked: https://repl.it/repls/UtterUnderstatedQuadrilal, "worked" right, parameter is incorrect, but method was found and executed.
– Daniel Mendes
@Saulolins, could also edit his reply by attaching the
tsconfig.json
? Typescript has very different behaviors depending on the configuration... Also check the version of Node.js installed and the package version@types/node
installed in this project.– Luiz Felipe
@Luizfelipe I checked here, and the typescript version that was installed, including the types/Node were very old, I do not know how this happened, since I started this project before yesterday, updated everything and started working normally again Thank you very much!
– Saulo Lins