Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda