Skip to content

Commit

Permalink
Fix: don't use deprecated API (#11689)
Browse files Browse the repository at this point in the history
* Fix: don't use deprecated API

* Update lib/util/relative-module-resolver.js

Co-Authored-By: Teddy Katz <[email protected]>
  • Loading branch information
mysticatea and not-an-aardvark committed May 10, 2019
1 parent 483239e commit aae6f65
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/util/relative-module-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@
const Module = require("module");
const path = require("path");

// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0)
const createRequireFromPath = Module.createRequireFromPath || (filename => {
const mod = new Module(filename, null);
// Polyfill Node's `Module.createRequire` if not present. We only support the case where the argument is a filepath, not a URL.
const createRequire = (

mod.filename = filename;
mod.paths = Module._nodeModulePaths(path.dirname(filename)); // eslint-disable-line no-underscore-dangle
mod._compile("module.exports = require;", filename); // eslint-disable-line no-underscore-dangle
return mod.exports;
});
// Added in v12.2.0
Module.createRequire ||

// Added in v10.12.0, but deprecated in v12.2.0.
Module.createRequireFromPath ||

// Polyfill.
(filename => {
const mod = new Module(filename, null);

mod.filename = filename;
mod.paths = Module._nodeModulePaths(path.dirname(filename)); // eslint-disable-line no-underscore-dangle
mod._compile("module.exports = require;", filename); // eslint-disable-line no-underscore-dangle
return mod.exports;
})
);

module.exports = {

Expand All @@ -30,7 +40,7 @@ module.exports = {
*/
resolve(moduleName, relativeToPath) {
try {
return createRequireFromPath(relativeToPath).resolve(moduleName);
return createRequire(relativeToPath).resolve(moduleName);
} catch (error) {
if (error && error.code === "MODULE_NOT_FOUND" && error.message.includes(moduleName)) {
error.message += ` relative to '${relativeToPath}'`;
Expand Down

0 comments on commit aae6f65

Please sign in to comment.