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

439
Vistas
ReferenceError: File is not defined - express

I`m trying to convert some base64 string to a image file and pass it to firebase via express.

Everything works fine on front end, except this part:

const convertBase64ToFile = (base64String, fileName) => {
  let arr = base64String.split(',');
  let mime = arr[0].match(/:(.*?);/)[1];
  let bstr = atob(arr[1]);
  let n = bstr.length;
  let uint8Array = new Uint8Array(n);
  while (n--) {
    uint8Array[n] = bstr.charCodeAt(n);
  }
  const file = new File([uint8Array], fileName, { type: mime }); /// getting Error in this line
  return file
}

Which library i have to import?

Error:

  const file = new File([uint8Array], fileName, { type: mime }); /// getting Error in this line
               ^

ReferenceError: File is not defined
    at convertBase64ToFile (C:\Users\rahto\devel\new\maissaudeapi\api\firestore\write.js:19:16)
    at conversor (C:\Users\rahto\devel\new\maissaudeapi\api\firestore\write.js:33:16)
    at C:\Users\rahto\devel\new\maissaudeapi\mainServer.js:31:21
    at Layer.handle [as handle_request] (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:346:12)
    at next (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:280:10)

Node.js v18.6.0
[nodemon] app crashed - waiting for file changes before starting...

Then, i changed to this:

const convertBase64ToFile = (base64String, fileName) => {
  let arr = base64String.split(',');
  let mime = arr[0].match(/:(.*?);/)[1];
  let bstr = atob(arr[1]);
  let n = bstr.length;
  let uint8Array = new Uint8Array(n);
  while (n--) {
    uint8Array[n] = bstr.charCodeAt(n);
  }
  const file = fs.writeFileSync(fileName, uint8Array)
  let fiz = fs.readFileSync(fileName, file);
  // const file = new File([uint8Array], fileName, { type: mime });
  return fiz
}

And got this error:

C:\Users\rahto\devel\new\maissaudeapi\node_modules\@firebase\storage\dist\index.node.cjs.js:3036
    const newPath = child(ref._location.path, childPath);
                                        ^

TypeError: Cannot read properties of undefined (reading 'path')
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It seems you are using node runtime. You can use fs module to access file system.

fs.writeFile( file, data, options, callback )

Parameters: This method accept four parameters as mentioned above and described below:

  • file: It is a string, Buffer, URL or file description integer that denotes the path of the file where it has to be written. Using a file descriptor will make it behave similar to fs.write() method.
  • data: It is a string, Buffer, TypedArray or DataView that will be written to the file.
  • options: It is an string or object that can be used to specify optional parameters that will affect the output. It has three optional parameter:
  • encoding: It is a string value that specifies the encoding of the file. The default value is ‘utf8’.
  • mode: It is an integer value that specifies the file mode. The default value is 0o666.
  • flag: It is a string value that specifies the flag used while writing to the file. The default value is ‘w’.
  • callback: It is the function that would be called when the method is executed.
    • err: It is an error that would be thrown if the operation fails.

Usage:

var fs = require('fs');

fs.writeFile('file.txt', 'Hello content!', function (err) {
  if (err) throw err;
  console.log('Saved!');
});

Also check more here and here for usage. And also check here for documentation.

about 4 years ago · Santiago Trujillo 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