From 2dea50ab1388f90fb0de40e83439d647e83db0a4 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Tue, 8 Jan 2019 11:04:40 -0600 Subject: [PATCH] fix(@embark/coderunner): use custom require function in vm context Supply a custom require function to the vm context for `doEval` so that module resolution succeeds for both DApp dependencies and Embark dependencies. --- src/lib/core/modules/coderunner/runCode.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib/core/modules/coderunner/runCode.js b/src/lib/core/modules/coderunner/runCode.js index 0f25cd676..03e02db92 100644 --- a/src/lib/core/modules/coderunner/runCode.js +++ b/src/lib/core/modules/coderunner/runCode.js @@ -6,11 +6,21 @@ const noop = function() {}; class RunCode { constructor({logger}) { this.logger = logger; + const customRequire = (mod) => { + return require(customRequire.resolve(mod)); + }; + customRequire.resolve = (mod) => { + return require.resolve( + mod, + {paths: [fs.dappPath('node_modules'), fs.embarkPath('node_modules')]} + ); + }; const newGlobal = Object.create(global); newGlobal.fs = fs; this.context = Object.assign({}, { - global: newGlobal, console, exports, require, module, __filename, __dirname, process, - setTimeout, setInterval, clearTimeout, clearInterval + global: newGlobal, console, exports, require: customRequire, module, + __filename, __dirname, process, setTimeout, setInterval, clearTimeout, + clearInterval }); }