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

246
Visualizações
Require is not defined after installed nodejs

First I dowloaded nodejs from link.

Then I installed browserify npm install -g browserify

Then I installed fs npm install fs

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
const fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
    if(err) {return console.log(err);}
    console.log("The file was saved!");
});
</script>
</body>
</html>

I am getting the following error:

Uncaught ReferenceError: require is not defined
    at index.html:12:12

Why require is still not defined? What could I do to get the code to be executable?

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

0

Node.js is a stand-alone environment for running JavaScript. It is not a browser extension.

To run Node.js code, save it in a file with a .js file extension, then run it with:

node yourFile.js

If you want Node.js code and browser code to interact then the typical way is to write a server in Node.js that hosts a web service. Express.js is a popular way to do this. You can then use Ajax to make HTTP requests to that web service.

For bi-directional communication (as opposed to the request/response of Ajax) look at Socket.io.


I just want it to be local storage, so I do not want to share the datas between the users, I just want to interact with there own local storage datas.

If you want to use the localStorage API provided by browsers, then do so.

localStorage doesn't need Node.js. It doesn't need the fs module that is built into Node.js. It doesn't need the require method that is part of the CommonJS module specification.

You can't muck around freely on the user's file system from code running in the browser. That would be a huge security problem.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can't use node APIs in browser environment; more specifically browser won't allow script to directly interact with file APIs like Node.

Either run your code in node as quentin said, or use browser file api

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