title: nodejs fs createWriteStream not working with file path prefix
body: <p>I am trying to call an API, loop through an array of images, assign unique names to each image in the array and then write them to a local directory. If I simplify the code I can write them to the root folder .I already created the sub folder manually, so it existed prior to running the function.</p>
  <p>Here is my basic function:</p>
  <pre><code>const imageFolder = './img';
  
  function downloadImage(url, filepath) {
      client.get(url, res =&gt; {
          res.pipe(fs.createWriteStream(`${imageFolder}/${filepath}`));
      });
  }
  
  ...make api call
  
  const imagesArray = generations.data.map(item =&gt; item.generation.image_path);
  
  imagesArray.forEach(item =&gt; {
      // const fileName = uuid.v4() + '.webp'; // trying to assign unique filename with uuid
      const fileName = new Date().getTime().toString() + '.webp'; // trying to assign unique filename with date object
      downloadImage(item, fileName);
  });
  </code></pre>
  <p>If I change</p>
  <pre><code>res.pipe(fs.createWriteStream(`${imageFolder}/${filepath}`));
  </code></pre>
  <p>to</p>
  <pre><code>res.pipe(fs.createWriteStream(filepath));
  </code></pre>
  <p>then it will work but just dumps the images in the root. I was thinking perhaps I was trying to concatenate a variable name with a string (for fileName + '.webp', but it is working in the root as mentioned. See attached image.</p>
  <p>I also tried adding the path into the actual function call inside the forEach loop like so</p>
  <pre><code>downloadImage(item, `${imageFolder}/${fileName}`);
  </code></pre>
  <p>I did wonder about needing the <code>__dirname</code> variable, or whether it could be a permissions issue, but I don't see any errors.</p>
  <p>I am assuming this is pretty straightforward.</p>
  <p><a href="https://i.stack.imgur.com/9lk2p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9lk2p.png" alt="enter image description here" /></a></p>