Does anyone have any insight on how the
require logic is implemented in the sandbox environments used for rules, hooks and custom DB scripts, and how this can be replicated locally?
The canirequire site provides snippets showing how the version should be included in the
require function in order to specify a particular version of a library to use. For example, for Axios, you can use any of the following:
require('firstname.lastname@example.org') require('email@example.com') require('firstname.lastname@example.org')
Alternatively you can just use
require('axios'); and a version of Axios is imported, though I prefer to define this explicitly so I can feel confident the default version won’t change under me and potentially break something.
The trouble is that by default if you run the code locally (e.g. in unit tests) you’ll get an error, because if you
npm install email@example.com it’ll install to
I’ve tried a couple of approaches to work around the issue:
- In the script, check if the client is in
globaland, if not, use
require('firstname.lastname@example.org')and add it to global. In unit tests I then
require('axios')and set it in the global, so the
requirein the main script isn’t invoked.
postinstallscripts to symlink the versioned package names with
"axios": "link email@example.com"
These both work but both feel a bit smelly. I’ve tried installing these packages with alias names but
@ can’t be used in aliases so that didn’t work. I’m guessing in the Auth0 sandbox either the packages are available at those paths, or Auth0 has overridden
require.resolve or similar, but I don’t have a great deal of experience with NodeJS or NPM and have been unable to do either in a way that I’m happy with.
Does anyone have any suggestions on how this could be done more nicely?