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

170
Visualizações
How to make JS read the contents of a local text file

I know using JS to read the contents of a .txt is possible.

For example, I have a text file code.txt which contains

Abc
Ghi
123466
bored
idk

And I have index.js. I would like to use the contents of code.txt to create an array into index.js.

So, in that example, I would like an array like this to appear in index.js.

const entcode = [
  "Abc",
  "Ghi",
  "123466",
  "bored",
  "idk",
];

Is there a way to do that?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

If you're using node.js you can use the File system api

const fs = require("fs");
let contents = fs.readFileSync("code.txt").toString().split(/\r?\n/);
console.log(contents);
about 4 years ago · Juan Pablo Isaza Relatório

0

Yes!

  1. Install Deno (a JavaScript/TypeScript runtime that works outside your browser)

  2. Read the file and create the array like this:

./index.js:

const text = (await Deno.readTextFile('./code.txt')).trim();
const entcode = text.split(/\r?\n/);
console.log(entcode);
  1. Run it:
$ deno run index.js
⚠️  ️Deno requests read access to "./code.txt". Run again with --allow-read to bypass this prompt.
   Allow? [y/n (y = yes allow, n = no deny)]  y
[ "Abc", "Ghi", "123466", "bored", "idk" ]
about 4 years ago · Juan Pablo Isaza Relatório

0

If you use jQuery you can get the data by the below code.

jQuery.get('http://localhost/code.txt', (data) => {
    const res = data;
});
about 4 years ago · Juan Pablo Isaza 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