title: How to convert local file path to a file::?/ url safely in node.js?
body: <p>I have local file paths (in node.js) and I need to convert them into <code>file://</code> urls.</p>
  
  <p>I'm now looking at <a href="https://en.wikipedia.org/wiki/File_URI_scheme" rel="noreferrer">https://en.wikipedia.org/wiki/File_URI_scheme</a> and I feel this must be a solved problem and somebody must have a snippet or npm module to do this.</p>
  
  <p>But then I try to search npm for this but I get so much cruft it is not funny (file, url and path are a search hit in like every package ever :) Same with google and SO.</p>
  
  <p>I can do this naïve approach</p>
  
  <pre><code>site = path.resolve(site);
  if (path.sep === '\\') {
      site = site.split(path.sep).join('/');
  }
  if (!/^file:\/\//g.test(site)) {
      site = 'file:///' + site;
  }
  </code></pre>
  
  <p>But I'm pretty sure that is not the way to go.</p>