Search code examples
javascripthtmlgoogle-chrome-extensionimporthtml-to-pdf

Not able to use neither require nor import in browser extension popup.js


I am making a browser extension that gets the code of a div and converts it into html. I have tried doing this with jspdf , html2pdf, but none of that worked perfectly.

So i used html-to-pdfmake but i cannot import it in my popup.js

var pdfMake = require("pdfmake/build/pdfmake");
var pdfFonts = require("pdfmake/build/vfs_fonts");
pdfMake.vfs = pdfFonts.pdfMake.vfs;
var htmlToPdfmake = require("html-to-pdfmake");
  • If imported like this it gives me the error
Uncaught ReferenceError: require is not defined

if imported using "import"

import pdfMake from "pdfmake/build/pdfmake";
import pdfFonts from "pdfmake/build/vfs_fonts";
pdfMake.vfs = pdfFonts.pdfMake.vfs;
import htmlToPdfmake from "html-to-pdfmake";

it gives me

Uncaught TypeError: Failed to resolve module specifier "pdfmake/build/pdfmake". Relative references must start with either "/", "./", or "../".

Any ideas how to import it . The package doesn't have any tutorials as it is unpopular.

I have tried

import htmlToPdfmake from "./node_modules/html-to-pdfmake";

but that doesn't work as well.


Solution

  • From where do you want load this modules?

    Maybe this will help you How to import ES6 modules in content script for Chrome Extension