Is it possible to require npm modules in a chrome extension?

I tried but I have a "require not defined" error. I can't find information on this, can someone enlighten the noob in me please?

+3


source to share


2 answers


It is possible, but you must be careful. Trying to require () package means that node will try to find its files on your filesystem. The chrome extension only has access to the files you declare in the manifest, not your filesystem.

To get around this, use a module like Webpack that will generate one javascript file containing all the code for all packages included via require (). You will need to create a separate module for each chrome extension component (for example, one for the background page, one for content scripts, one for the popup) and declare each generated module in your manifest.



Rather than trying to tweak the build system to make using require () possible, I suggest starting with a template project . You can check my extension to see how I do it.

+5


source


There is no way to require node modules directly in a chrome extension. However, it is possible to bundle node apps and packages into a browser for use with your extensions. See here for more details: Is it possible to develop Google Chrome extensions using node.js?



+1


source







All Articles