Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Can't find/load SheetJS XLSX library (xlsx.full.min.js)

stackdevlopr

New Coder
Hello,

I have tried couple of ways to tackle this issue I'm experiencing with reading SheetJS XLSX library, but don't know what exactly is causing this issue: JS? CSP? Browser policies?

It is a simple app to process Excel files using the SheetJS XLSX library (xlsx.full.min.js):
_ if the app is opened from netlify.app, the library should load from CDN => no issues here
_ if it is saved an opened locally, the library that is included within the same folder as the index.html must be called => this is the part that is not working and consolelogging that the library can't be found.

I found various possible explanations and suggestions to deal with this, but none worked.

In my HTML head I have CSP (Content-Security-Policy) for protection against unsafe inlie scripting:
HTML:
<meta
      http-equiv="Content-Security-Policy"
      content="default-src 'self'; script-src 'self' https://cdn.sheetjs.com; style-src 'self' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com;"
    />

The library isn't referenced in the HTML head, as it is dynamically loaded in JS, here is the logic to load the library from CDN or locally, depending on the app being accessed online or locally:
JavaScript:
// Paths to library
const onlineLibraryPath = "https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js";
const localLibraryPath = "./xlsx.full.min.js";

// Toggle for local vs. online mode
const isOnlineMode = false;

document.addEventListener("DOMContentLoaded", () => {
  let libraryPath;

  if (isOnlineMode) {
      // Check and clear content if unauthorized access
    if (!location.hostname.includes("netlify.app")) {
      alert("This app can only be opened from netlify.app in online mode.");
      document.body.innerHTML = "";
      return;
    }
    libraryPath = onlineLibraryPath;
  } else {
    libraryPath = localLibraryPath;
  }

  // Dynamically load the library
  loadLibrary(libraryPath).then(() => {
    console.log(`Library loaded successfully from: ${libraryPath}`);
  }).catch(err => {
    console.error("Failed to load library:", err);
    alert("An error occurred while loading the required library. Please check your setup.");
  });
});

function isAuthorizedOnline() {
  const authorizedHostnames = ["netlify.app"];
  return authorizedHostnames.some((host) => location.hostname.includes(host));
}

// Load external library dynamically
// ..Check if the script is already loaded
// ..Append script to document head
function loadLibrary(src) {
  return new Promise((resolve, reject) => {
    const existingScript = document.querySelector(`script[src="${src}"]`);
    if (existingScript) {
      console.log(`Library already loaded from: ${src}`);
      resolve();
      return;
    }

    const script = document.createElement("script");
    script.src = src;
    script.async = true;

    // Successful loading
    script.onload = () => {
      console.log(`Library successfully loaded: ${src}`);
      resolve();
    };

    // Error loading
    script.onerror = (err) => {
      console.error(`Failed to load library from: ${src}`, err);
      reject(err);
    };

    document.head.appendChild(script);
  });
}
Calling the locally included library should be a straightforward thing, but apparently CSP or more likely browsers policies are blocking the access to the local file, as I understood. No issues with loading the app.js file on the other hand.

Anyone had to deal with similar issue and could suggest a way?

Thanks already for the help.
 
Last edited:
Hello,

I have tried couple of ways to tackle this issue I'm experiencing with reading SheetJS XLSX library, but don't know what exactly is causing this issue: JS? CSP? Browser policies?

It is a simple app to process Excel files using the SheetJS XLSX library (xlsx.full.min.js):
_ if the app is opened from netlify.app, the library should load from CDN => no issues here
_ if it is saved an opened locally, the library that is included within the same folder as the index.html must be called => this is the part that is not working and consolelogging that the library can't be found.

I found various possible explanations and suggestions to deal with this, but none worked.

In my HTML head I have CSP (Content-Security-Policy) for protection against unsafe inlie scripting:
HTML:
<meta
      http-equiv="Content-Security-Policy"
      content="default-src 'self'; script-src 'self' https://cdn.sheetjs.com; style-src 'self' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com;"
    />

The library isn't referenced in the HTML head, as it is dynamically loaded in JS, here is the logic to load the library from CDN or locally, depending on the app being accessed online or locally:
JavaScript:
// Paths to library
const onlineLibraryPath = "https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js";
const localLibraryPath = "./xlsx.full.min.js";

// Toggle for local vs. online mode
const isOnlineMode = false;

document.addEventListener("DOMContentLoaded", () => {
  let libraryPath;

  if (isOnlineMode) {
      // Check and clear content if unauthorized access
    if (!location.hostname.includes("netlify.app")) {
      alert("This app can only be opened from netlify.app in online mode.");
      document.body.innerHTML = "";
      return;
    }
    libraryPath = onlineLibraryPath;
  } else {
    libraryPath = localLibraryPath;
  }

  // Dynamically load the library
  loadLibrary(libraryPath).then(() => {
    console.log(`Library loaded successfully from: ${libraryPath}`);
  }).catch(err => {
    console.error("Failed to load library:", err);
    alert("An error occurred while loading the required library. Please check your setup.");
  });
});

function isAuthorizedOnline() {
  const authorizedHostnames = ["netlify.app"];
  return authorizedHostnames.some((host) => location.hostname.includes(host));
}

// Load external library dynamically
// ..Check if the script is already loaded
// ..Append script to document head
function loadLibrary(src) {
  return new Promise((resolve, reject) => {
    const existingScript = document.querySelector(`script[src="${src}"]`);
    if (existingScript) {
      console.log(`Library already loaded from: ${src}`);
      resolve();
      return;
    }

    const script = document.createElement("script");
    script.src = src;
    script.async = true;

    // Successful loading
    script.onload = () => {
      console.log(`Library successfully loaded: ${src}`);
      resolve();
    };

    // Error loading
    script.onerror = (err) => {
      console.error(`Failed to load library from: ${src}`, err);
      reject(err);
    };

    document.head.appendChild(script);
  });
}
Calling the locally included library should be a straightforward thing, but apparently CSP or more likely browsers policies are blocking the access to the local file, as I understood. No issues with loading the app.js file on the other hand.

Anyone had to deal with similar issue and could suggest a way?

Thanks already for the help.
Hi there,
I don't believe it's an CSP issue... have you tried using the absolute file path and seeing what happens? Also, what OS are you running this under? HINT HINT on file path formatting 😉
 
Hi there,
I don't believe it's an CSP issue... have you tried using the absolute file path and seeing what happens? Also, what OS are you running this under? HINT HINT on file path formatting

Hello,

Thank you for your response.

I also don’t believe the issue is related to the CSP. Even after commenting it out, I’m still experiencing the same problem. However, since I haven’t been able to find any other solution or explanation, I considered whether it might be part of the problem.

I’ve already tried using absolute paths. All the files, including HTML, CSS, JS, and the library, are in the same folder. Yet, even when I copy-paste the exact path to the library, it still doesn’t work.

Folder setup:
/App/
├── index.html
├── app.js
├── xlsx.full.min.js
├── styles.css

d:/__2025__FILES/WEBDEV/__JS/__JS__APPS/App/xlsx.full.min.js

When I save the app locally as a complete web page, the browser automatically separates the dependent files into a folder called index_files. And when I look in this folder, the .js-files seem to be remaining as incomplete downloads... browsers restrictions?
file:///C:/Users/Admin/Downloads/index_files/xlsx.full.min.js

However, the error suggests that the library is not being searched in the index_files folder:
GET file:///C:/Users/Admin/Downloads/xlsx.full.min.js net::ERR_FILE_NOT_FOUND

From what I’ve gathered, this may be due to modern browsers blocking the loading of locally included libraries. However, there also seems to be a mismatch between the saved file structure and the error path, which could be contributing to the issue.

To make the confusion complete, everything seem to be working just fine, despite this loading error message, but this could be due to an earlier cached file or is the error message wrongly generated?

I’m using Windows 11 and have tested on multiple browsers, all with the same results.

Is there a straightforward solution to this problem?

Greatly appreciated!
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom