2
let path = "/var/www/backend/src/foo";
console.log("a", path, require("/var/www/backend/src/foo"));
console.log("b");
console.log(require(path)); // throws an error
console.log("c");

I have this code inside some npm module, I am able to require a file with absolute path given as string, but when I do the same with a variable, I get the following error:

undefined:688
    console.log(!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND';; throw e; }()));
Error: Cannot find module "."

and the output is:

a /var/www/backend/src/foo { default: [Function: foo] }
b
undefined:688
    console.log(!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND';; throw e; }()));

What's wrong? Why is that so?

user99999
  • 1,994
  • 5
  • 24
  • 45
  • Possible duplicate of [webpack can not require variable ,the request of a dependency is an expression](http://stackoverflow.com/questions/31694346/webpack-can-not-require-variable-the-request-of-a-dependency-is-an-expression) – ponury-kostek Feb 27 '17 at 11:18

0 Answers0