Search code examples
apostrophe-cms

Push JS from npm module to browser in ApostropheCMS


Is it possible to push JS from an npm module to the browser in ApostropheCMS?

The file lib/modules/my-module/public/js/custom.js has been pushed to the browser with pushAsset successfully.

Three.js has been installed with npm i three.

Now THREE needs to get to the browser somehow for import, as described here.

I need to do this browserside:

var THREE = require('three');

Solution

  • require is a feature that comes with build tools like webpack or browserify. Those tools are not standard parts of apostrophe, nor are they built into browsers. You need to use a tool like webpack to compile code that contains require statements. So what you do instead is push the output files of your webpack pipeline as assets in apostrophe.