Skip to content
Permalink
Browse files

Fix: don't use deprecated API (#11689)

* Fix: don't use deprecated API

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

Co-Authored-By: Teddy Katz <teddy.katz@gmail.com>
  • Loading branch information...
mysticatea and not-an-aardvark committed May 10, 2019
1 parent 483239e commit aae6f6525557ba06e73f051511646056313fcf91
Showing with 19 additions and 9 deletions.
  1. +19 −9 lib/util/relative-module-resolver.js
@@ -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 = {

@@ -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}'`;

0 comments on commit aae6f65

Please sign in to comment.
You can’t perform that action at this time.