I'm having trouble with a jszip configuration [duplicate]

I’m trying to hook up jszip to my Flask/python project running in VS on windows, but JS crashes as soon as I try to import jszip or declare an instance with the ‘var JSZip = require(“jszip”);’ call. I’ve tried both downloading and extracting the zip installer as well as installing with npm. Npm installs with success and node is in the path. The npm seem to create a folder within the ‘static’ called ‘node_modules\jszip’ while i’ve unzipped the one I downloaded to ‘scripts\jszip’. When I either add the ‘import’ declaration or the ‘var JSZip = require(“jszip”);’ line, the js function crashes. Same if I only call the top folder 'import JSZip from ‘node_modules/jszip’. Without the ‘import’ nor the ‘require’ call, the Alert displays. I’m a new to js modules, so it’s probably a very basic error but I just cannot spot it. Any help appreciated :slight_smile:

import JSZip from 'node_modules/jszip/dist/jszip.js';

function download_zip(arrUrls) {
    alert("1");
    var JSZip = require("jszip");
}

It sounds like you are facing an issue related to how JavaScript modules are being loaded and used in your project. Since you’re using Flask with Python and attempting to integrate JSZip, you need to be mindful of the way the JS module system works in browsers and how you are including JS libraries.

Here are a few things you can try to solve this issue:

1. Ensure JSZip is Correctly Included

If you’re trying to use JSZip in the front-end (i.e., in the browser), make sure the JSZip library is correctly included in your HTML file. There are a few ways to do this, and I’ll explain them below:

Option 1: Use a CDN (Recommended for Frontend Usage)

This is the simplest approach where you don’t need to worry about managing node modules, and you can directly link to a CDN (Content Delivery Network) to load JSZip.

In your HTML file (where you want to use JSZip), include the CDN link:

<script src="https://cdn.jsdelivr.net/npm/jszip/dist/jszip.min.js"></script>
<script>
  function download_zip(arrUrls) {
    alert("JSZip is loaded");
    var zip = new JSZip(); // Initialize the JSZip instance
    // Your code to work with zip...
  }
</script>

This will load JSZip from the CDN and allow you to use it directly in the browser without worrying about npm or module bundlers like Webpack.

Option 2: Use require() or import (When using Webpack, Parcel, or other bundlers)

If you want to use npm and install JSZip using npm (which seems like what you tried), make sure you have a module bundler like Webpack or Parcel in place, as they allow you to use import and require in the front-end code. Here’s how you can do it:

  1. Install JSZip using npm:
npm install jszip
  1. Make sure you have a bundler (e.g., Webpack) configured to bundle your JavaScript files. This requires setting up a basic Webpack configuration if you don’t already have one.
  2. In your front-end JavaScript file, use the import statement (with Webpack or Parcel configured):
import JSZip from 'jszip';  // Import JSZip using ES module syntax
  1. Or you can use require() if you’re using CommonJS syntax:
var JSZip = require("jszip");  // Import JSZip using CommonJS syntax
  1. Use the imported JSZip to create a zip instance and work with it:
function download_zip(arrUrls) {
  alert("JSZip is loaded");
  var zip = new JSZip();  // Create an instance of JSZip
  // Your logic for working with zip files goes here...
}

2. Make Sure to Include node_modules in the Right Place (for require)

If you are using require() or import directly in the browser, it’s crucial to understand that the browser doesn’t natively understand require or import (unless you’re using a bundler like Webpack, as mentioned above).

If you are trying to reference files from node_modules, the browser won’t automatically look in that directory unless you set up Webpack or another module bundler.

So, you can’t directly do something like this:

import JSZip from 'node_modules/jszip/dist/jszip.js';

3. Flask and Static Files (Backend Considerations)

Since you’re using Flask, your Python backend serves the static folder. Ensure that your JavaScript files, as well as node_modules, are correctly served by Flask. However, Flask typically doesn’t serve node_modules directly to the browser. You should either:

  • Copy the necessary files from node_modules into your static folder (or any folder where you want to serve your frontend assets).
  • Or use a build tool like Webpack to bundle your JavaScript and its dependencies, and serve the bundled files.

4. Using Flask to Serve JS Files (Basic Example)

Here is an example of serving the required JS files in Flask:

Flask Backend Example:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

Frontend HTML Template (index.html):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>JSZip Example</title>
  <!-- Link to JSZip CDN -->
  <script src="https://cdn.jsdelivr.net/npm/jszip/dist/jszip.min.js"></script>
</head>
<body>
  <button onclick="download_zip()">Download Zip</button>

  <script>
    function download_zip() {
      var zip = new JSZip(); // Create a JSZip instance
      zip.file("hello.txt", "Hello, World!"); // Add a file to the zip
      zip.generateAsync({ type: "blob" })
        .then(function(content) {
          saveAs(content, "example.zip"); // Trigger download
        });
    }
  </script>
</body>
</html>

In this example, JSZip is included from the CDN, so no need to worry about modules or npm for a simple setup.

Conclusion

  • For simplicity: Use a CDN to include JSZip directly in your HTML file, especially if you’re building a frontend-only application.
  • For more advanced setups: Use npm with Webpack or another module bundler to handle JSZip as a dependency.
  • Flask integration: Ensure your static files (JavaScript, CSS) are correctly served via Flask’s static folder.

If you’re just starting with JSZip, I recommend trying the CDN method first to avoid dealing with module bundlers until you’re more comfortable with the setup. Let me know if you need further clarification!