Hi, I’m trying to resolve a `require(…)` problem. ...
# help
e
Hi, I’m trying to resolve a
require(…)
problem. Seems like it’s probably a bundling problem specific to sst or esbuild, and it’s beyond my skill-set so far… so any help would be awesome.   I have some code running in a lambda configured using the
sst.Queue
construct. It is calling a library that uses
proxy-agent
under the hood, loaded via
require('proxy-agent');
. This library has a transitive dependency on
vm2
(to run the PAC file, I assume).   When run with unit tests, this library call runs fine. When run under
npx sst start
, the
require
call fails like this:  
Copy code
Error: ENOENT: no such file or directory, open '/Users/asquithea/repos/deploy-controller/.sst/artifacts/aff14e6d/src/handlers/contextify.js'
   at Object.openSync (node:fs:585:3)
   at Object.readFileSync (node:fs:453:35)
   at loadAndCompileScript (/Users/asquithea/repos/deploy-controller/node_modules/vm2/lib/main.js:49:18)
   at node_modules/vm2/lib/main.js (/Users/asquithea/repos/deploy-controller/node_modules/vm2/lib/main.js:76:20)
   at __require (/Users/asquithea/repos/deploy-controller/.sst/artifacts/aff14e6d/src/handlers/command.js:26:44)
   at node_modules/vm2/index.js (/Users/asquithea/repos/deploy-controller/node_modules/vm2/index.js:3:18)
   at __require (/Users/asquithea/repos/deploy-controller/.sst/artifacts/aff14e6d/src/handlers/command.js:26:44)
   at node_modules/degenerator/dist/src/index.js (/Users/asquithea/repos/deploy-controller/node_modules/degenerator/src/index.ts:6:1)
   at __require (/Users/asquithea/repos/deploy-controller/.sst/artifacts/aff14e6d/src/handlers/command.js:26:44)
   at node_modules/pac-resolver/dist/index.js (/Users/asquithea/repos/deploy-controller/node_modules/pac-resolver/src/index.ts:3:1)
  The
artifacts
directory contains only the compiled lambda (
command.ts
->
command.js
).  
contextify.js
is part of the
vm2
library, which tries to load it dynamically, like this:
Copy code
contextifyScript: loadAndCompileScript(`${__dirname}/contextify.js`, '(function(require, host) { ', '\n})'),
  So this looks like a bundling problem: I guess the
vm2
library assumes a standard
node_modules
structure.  I see there are some control options for bundling in the construct, but at this point I don’t really know what I’m doing. Is this similar enough to someone else’s problem that you can offer any advice? TIA.
o
Not too experienced in debugging this type of issue myself - but maybe this
nodeModules
bundling option will help: https://docs.serverless-stack.com/constructs/Function#nodemodules
e
Success! I tried using the
copyFiles
option initially. That appears to put the file in the desired location when running
npx sst build
but not with
npx sst start
. However adding
proxy-agent
to the
node_modules
option does appear to have solved the problem. Many thx. 🌟