title: converting image to base64 with data-uri with typescript
body: <p>I successfully upload images with multer, store them in the local directory, save the path to the database. But since heroku does not save images in the local directory I have to upload images to Cloudinary but Cloudinary needs file to be converted to base64.</p>
  <p>This is the implementation in node.js:</p>
  <pre class="lang-js prettyprint-override"><code>const path = require(&quot;path&quot;);
  const DataUri = require(&quot;datauri&quot;);
  
  const dUri = new DataUri();
  
  exports.dataUri = (file: Express.Multer.File): string =&gt;
    dUri.format(path.extname(file.originalname).toString(), file.buffer);
  
  </code></pre>
  <p>I tried to implement this in typescript like this:</p>
  <pre class="lang-js prettyprint-override"><code>import path from &quot;path&quot;;
  import DataUri from &quot;datauri&quot;;
  // here is cauinsg issue
  const dUri = new DataUri();
  
  export const dataUri = (file: Express.Multer.File): string =&gt;
    dUri.format(path.extname(file.originalname).toString(), file.buffer);
  
  
  </code></pre>
  <p>in typescript implementation I am getting typescript error for <code>const dUri = new DataUri()</code> :</p>
  <pre><code>(alias) function DataUri(fileName: string, handler?: DataURI.Callback | undefined): Promise&lt;string | undefined&gt;
  
  Expected 1-2 arguments, but got 0.ts(2554)
  index.d.ts(2, 31): An argument for 'fileName' was not provided.
  'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
  </code></pre>
  <p>I dont understand why <code>new DataUri()</code> works in olain node but not in typescript. I thought it would be ts file error so ignored it but it did not work. When I start the app I got this error:&quot;  UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The &quot;path&quot; argument must be of type string or an instance of Buffer or URL. Received undefined&quot;</p>
  <p>Instead of creating function, I tried to implement like this in the controller:</p>
  <pre><code> const image = req.file;
    const file64=new DataUri(image.buffer)
  </code></pre>
  <p>this did not work neither</p>