Fs.Promises does not exist

Asked

Viewed 118 times

0

I was setting up an app when I came across the following mistake:

inserir a descrição da imagem aqui

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

  • 1

    Saul, try to post the full code, not an image, so maybe we can help you

  • Ready @Patrickperdigão

  • Have you tried import * as fs from 'fs';?

  • @Rafaeltavares Here to documentation with the President

  • @Leandrade I tried yes, no fuciona

  • @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

  • @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

  • @Rafaeltavares the error that returned was : Não é possível localizar o módulo 'fs/promises' ou suas declarações de tipo correspondentes.

  • Using require worked: https://repl.it/repls/UtterUnderstatedQuadrilal, "worked" right, parameter is incorrect, but method was found and executed.

  • 1

    @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.

  • @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!

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.