Only available in the CLI¶
At the moment you can only add libraries to your Pack when building with the Packs command line tool (CLI). If you are using the browser-based Pack Studio and want to use a library you'll have to migrate to the CLI workflow. See the CLI getting started guide for information on how to install and setup the CLI.
To add a library to your Pack simply use
npm install lodash
You can then import this library into your code using either Node's
require or the more modern
const _ = require('lodash'); // ... or ... import _ from 'lodash';
How to import the library may vary; consult the library's documentation for the correct syntax.
Unfortunately there isn't an easy way to determine beforehand if a given library will fail due to an unavailable API. At the moment the best approach is to try the library and see if it runs successfully in the Packs environment.
While many compatibility issues will be caught when the Pack is being built, there are others that are only exposed at runtime. In those cases your code will fail with an error like
<function> is not defined. The
code execute command runs your code in a VM with all of the same limitations in place, allowing you test compatibility without needing to upload your code to the server.
When using the CLI, browserify is used to provide shims for some Node.js modules. These shims themselves are designed to work in the browser, so not all modules that browserify supports may be supported in the Packs execution environment.
Additionally, the CLI provides optional shims for some timer-related functions (
setInterval). To enable these shims, pass the flag
--timerStrategy=fake when executing and uploading your Pack. Note that these shims attempt to approximate the behavior of these methods, but may not work reliably.