Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

491
Visualizações
fs/promises api in typescript not compiling in javascript correctly

I was recently working with typescript and fs/promises API and got this error when the typescript code was compiled and run, I got this error saying
internal/modules/cjs/loader.js:968
  throw err;
  ^

Error: Cannot find module 'fs/promises'

when I looked at the compiled code this is what I found...

var promises_1 = require("fs/promises");

which started working by changing it to

var promises_1 = require("fs").promises;

This is my typescript file import

import { readFile, writeFile, appendFile } from "fs/promises";

my package.json file

 "devDependencies": {
    "@types/cheerio": "^0.22.22",
    "@types/got": "^9.6.11",
    "@types/node": "^14.14.6"
  },
  "dependencies": {
    "cheerio": "^1.0.0-rc.3",
    "got": "^11.8.0"
  }

and my tsconfig.json file

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "outDir": "dist",
    "sourceMap": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

node version 12.18.0

Is there someone facing the same problem or am I doing something wrong?
please help
Thanks...

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The error was due to unsupported Node version 12.x which doesn't support this require statement...

var promises_1 = require("fs/promises");

but this works

var promises_1 = require("fs").promises;

This can be solved by updating the Node to the latest.

over 4 years ago · Santiago Trujillo Relatório

0

The solution I like best:

import { promises as fs } from 'fs';

async function main() {
  await fs.writeFile('test.txt', 'hello');
}
over 4 years ago · Santiago Trujillo Relatório

0

What worked for me in node v12.18.1 was:

import { promises } from "fs";
const { readFile, writeFile } = promises;

I could use both methods within nestJS/express context.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda