Skip to content

Commit ef2ecf5

Browse files
Akryumyyx990803
authored andcommitted
fix: require.resolve fallback on node < 8.10.0 (#1404)
close #1369
1 parent 4f39461 commit ef2ecf5

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

packages/@vue/cli/lib/util/module.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
1+
const semver = require('semver')
2+
3+
function resolveFallback (request, options) {
4+
const Module = require('module')
5+
const isMain = false
6+
const fakeParent = new Module('', null)
7+
8+
const paths = []
9+
10+
for (let i = 0; i < options.paths.length; i++) {
11+
const path = options.paths[i]
12+
fakeParent.paths = Module._nodeModulePaths(path)
13+
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true)
14+
15+
if (!paths.includes(path)) paths.push(path)
16+
17+
for (let j = 0; j < lookupPaths.length; j++) {
18+
if (!paths.includes(lookupPaths[j])) paths.push(lookupPaths[j])
19+
}
20+
}
21+
22+
const filename = Module._findPath(request, paths, isMain)
23+
if (!filename) {
24+
const err = new Error(`Cannot find module '${request}'`)
25+
err.code = 'MODULE_NOT_FOUND'
26+
throw err
27+
}
28+
return filename
29+
}
30+
31+
const resolve = semver.satisfies(process.version, '>=8.10.0') ? require.resolve : resolveFallback
32+
133
exports.resolveModule = function (request, context) {
234
let resolvedPath
335
try {
4-
resolvedPath = require.resolve(request, {
36+
resolvedPath = resolve(request, {
537
paths: [context]
638
})
739
} catch (e) {}

0 commit comments

Comments
 (0)