0

I have a Flask application I have built locally and have hit the wall as to having javascript with npm packages being able to be used because I am consuming REST APIs and using jQuery per my application. I followed the advice here and have Webpack working.

I ran the command npm run watch

    webpack --progress -d --config webpack.config.js --watch

  0% compiling
Webpack is watching the files…

Version: webpack 2.1.0-beta.21
Time: 4836ms

    Asset     Size  Chunks             Chunk Names

bundle.js  5.97 MB       0  [emitted]  main
[366] ./js/index.js 15.3 kB {0} [built]
   + 366 hidden modules

So now I have webpack working with my index.js file and I have the Flask app being served using gunicorn and Docker on 5000.

Everything is working, but my JS is still unresponsive in my Flask application.

Does anyone know what I do to have my index.js file to work per this Flask app with these components I have, or what I am doing incorrectly here?

Here is my html template, rendering just fine. Just not any Javascript:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description"
          content="{% block meta_description %}{% endblock %}">

    <title>{% block title %}{% endblock %}</title>

    <link
      rel="stylesheet"
      href="{{ url_for('static', filename='styles/vendor/bootstrap.min.css') }}">
    <link
      rel="stylesheet"
      href="{{ url_for('static', filename='styles/main.css') }}">

    <link
      rel="stylesheet"
      href="{{ url_for('static', filename='styles/style.css') }}">

    <link
      rel="stylesheet"
      href="{{ url_for('static', filename='styles/watson.css') }}">

    <link
      rel="stylesheet"
      href="{{ url_for('static', filename='styles/vendor/font-awesome.min.css') }}">

    <script src="https://code.jquery.com/jquery-1.12.2.min.js"></script>

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

  </head>
  <body>
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed"
                  data-toggle="collapse" data-target="#navbar"
                  aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a href="{{ url_for('page.home') }}">
          </a>
        </div>
      </div>
    </nav>

    <main class="container">
      <div class="md-margin-top">{% block heading %}{% endblock %}</div>
      {% block body %}{% endblock %}
    </main>

    <footer class="footer text-center">
      <div class="container">
        <ul class="list-inline">
          <li class="text-muted">2018</li>
          <li><a href="{{ url_for('page.privacy') }}">NMT</a></li>
          <li><a href="{{ url_for('page.terms') }}">How it works</a></li>
        </ul>
      </div>
    </footer>

    <script
      src="{{ url_for('static', filename='scripts/vendor/bootstrap.min.js') }}">    
    </script>

    <script src="{{ url_for('static', filename='js/bundle.js') }}"></script>

    <script src="{{ url_for('static', filename='node_modules/watson-developer-cloud/v2.js') }}"></script>


   <script  src="{{ url_for('static', filename='js/index.js') }}"></script>


  </body>
</html>

The parts involved in the question: https://jsfiddle.net/cgnws2u3/3/

Error Logs

I updated filepath and now get this per my js files I am trying to link to my project:

Uncaught ReferenceError: require is not defined
at v2-generated.ts:17
John Veridan
  • 71
  • 1
  • 10

1 Answers1

0

Make sure that your Flask project structure contains static folder as per Flask documentation.

According to the relative bundle.js file path in index.html template, your static directory should contain js directory. Make sure you have it there after using webpack and it contains your bundle.js, index.js and the rest of files mentioned in your template.

Alex
  • 2,074
  • 1
  • 15
  • 14