Using an imported module inside Google App Script

Your GAS project is not a Node.js app, so the above will not work. While both Google Apps Script and Node use JavaScript, they provide different runtime environments for executing JS code. In GAS, the environment is a closed ecosystem on Google Servers that end users don’t know anything about.

In Node, the runtime consists of V8 (JS engine) and C++ add-ons that expose low-level APIs (access to the file system, etc.). The library you referenced is an NPM package created for Node.js. Installing the package via NPM will make it available for Node projects, but don’t think it will magically appear on Google servers as well.

You must either use GAS-specific versions of these dependencies, or, if they don’t exist, refactor the source code to make it compatible with GAS (Node and GAS use different ECMAScript versions, so some latest features like arrow functions will break your GAS code).

For example, here’s lodash for GAS
https://github.com/contributorpw/lodashgs

Using libraries in GAS
https://developers.google.com/apps-script/guides/libraries

P.S. In GAS, all “.gs” files share the same namespace, so calling ‘require’ function is redundant. If you want to mimic this behavior, you’ll still need to write your own require function.

Leave a Comment