title: Flask: How to use ES6 modules?
body: <p>I have a working Flask app that I'm trying to refactor to use ES6 imports.  I don't need it to run on old browsers, and ES6 imports work in modern browsers without transpilation, right?</p>
  
  <p>I'm just running this via Flask's built-in server at the moment.  The production app is served via gevent instead, but I'm obviously not at that point with these changes yet.</p>
  
  <p>Below is what I've tried so far.  Where have I gone wrong?</p>
  
  <p>views.py</p>
  
  <pre><code>@app.route('/home')
  def serve_home():
      return render_template('home.html')
  </code></pre>
  
  <p>formatting.js</p>
  
  <pre><code>export function formatNumber(...) {
    ...
  }
  </code></pre>
  
  <h2>Attempt 1</h2>
  
  <p>home.html</p>
  
  <pre><code>&lt;script type="text/javascript" src="/static/js/main.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>main.js</p>
  
  <pre><code>import {formatNumber} from "/static/js/formatting.js";
  </code></pre>
  
  <p>Error (main.js, line 1)</p>
  
  <blockquote>
    <p>Uncaught SyntaxError: Unexpected token {</p>
  </blockquote>
  
  <h2>Attempt 2</h2>
  
  <ul>
  <li>Changed the script type to "module"</li>
  </ul>
  
  <p>home.html</p>
  
  <pre><code>&lt;script type="module" src="/static/js/main.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>Error (main.js, line 1)</p>
  
  <blockquote>
    <p>Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.</p>
  </blockquote>
  
  <h2>Attempt 3</h2>
  
  <ul>
  <li>Changed the extension of each of the two Javascript files from "js" to "mjs"</li>
  </ul>
  
  <p>home.html</p>
  
  <pre><code>&lt;script type="module" src="/static/js/main.mjs"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>main.mjs</p>
  
  <pre><code>import {formatNumber} from "/static/js/formatting.mjs";
  </code></pre>
  
  <p>Error (main.mjs, line 1)</p>
  
  <blockquote>
    <p>Failed to load module script: The server responded with a non-JavaScript MIME type of "application/octet-stream". Strict MIME type checking is enforced for module scripts per HTML spec.</p>
  </blockquote>