Making a variable global?

Hello, everyone!

I’ve been trying to load some npm modules in Scriptable to make development easier and code shorter.
With importModule, I was able to load most of the modules I want so far.

Though, here is the challenge.
Some npm modules have require() statements to load its internal modules.
This would fail with RefferenceError as require variable is not found.

I tried adding this one liner at the top of my file before loading the module

require = importModule
let get = require(‘lib/lodash/get’)

thinking that might make require function global so that it would be available while evaluating the module’s source code; it failed.

I’m trying to understand why it doesn’t work the way I want.
Also, is it possible to make a variable global and be available in other Scriptable files while running other module source via importModule?

A help would be greatly appreciated!

I don’t think that there is a way to define globals over multiple scripts. The reason may be that every script runs in its own Javascript environment and each environment has its own globals.

You have two options what you can do (that come to my mind):

  1. Edit every file you have downloaded via npm and add let require = importModule at the very beginning. You don’t have to do this manually. Maybe make a Scriptable script, or add a script in the postinstall key in package.json > scripts.
  2. Package your downloaded modules with browserify or webpack. You would have to look for options in these to output them as modules.

Option #2 was what I’ve thought first, but I am a little hesitant to do that because of maintainability reason. Not that those modules need frequent update or anything, but I was wondering if this could be done easier/simpler way.

Thanks for your opinion!