diff --git a/lib/run.js b/lib/run.js index 184dec7dc..5b08007ae 100644 --- a/lib/run.js +++ b/lib/run.js @@ -16,8 +16,6 @@ module.exports = { if (purge) { utils.purgeCache(handlerFilePath); } - //set environment before requiring, as imported modules will be immediately - this.setEnvironmentVars(functionId); const module = require(handlerFilePath); const functionObjectPath = handler.slice(1); let func = module; diff --git a/lib/serve.js b/lib/serve.js index e2abf1db0..62aecbd25 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -20,6 +20,12 @@ module.exports = { throw err; } const loadedModules = []; + // We need to set all environments before requiring any handlers, as + // imported modules may be in a single file and immediately require + // their environment. + for (let funcConf of funcConfs) { + this.setEnvironmentVars(funcConf.id); + } for (let funcConf of funcConfs) { funcConf.handlerFunc = this.loadHandler( stats,