2
2
3
3
const {
4
4
ArrayIsArray,
5
+ ArrayPrototypeJoin,
5
6
JSONParse,
6
7
ObjectDefineProperty,
8
+ StringPrototypeEndsWith,
7
9
} = primordials ;
10
+ const {
11
+ codes : {
12
+ ERR_INVALID_ARG_VALUE ,
13
+ } ,
14
+ } = require ( 'internal/errors' ) ;
8
15
const modulesBinding = internalBinding ( 'modules' ) ;
9
- const { resolve } = require ( 'path' ) ;
16
+ const path = require ( 'path' ) ;
10
17
const { kEmptyObject } = require ( 'internal/util' ) ;
11
18
const { fileURLToPath, URL } = require ( 'internal/url' ) ;
12
19
const {
@@ -111,7 +118,7 @@ function read(jsonPath, { base, specifier, isESM } = kEmptyObject) {
111
118
*/
112
119
function readPackage ( requestPath ) {
113
120
// TODO(@anonrig): Remove this function.
114
- return read ( resolve ( requestPath , 'package.json' ) ) ;
121
+ return read ( path . resolve ( requestPath , 'package.json' ) ) ;
115
122
}
116
123
117
124
/**
@@ -122,10 +129,26 @@ function readPackage(requestPath) {
122
129
* @returns {undefined | DeserializedPackageConfig<everything> }
123
130
*/
124
131
function getNearestParentPackageJSON ( startLocation , everything = false ) {
125
- const startPath = URL . canParse ( startLocation ) ? fileURLToPath ( startLocation ) : startLocation ;
132
+ let startPath = URL . canParse ( startLocation ) ? fileURLToPath ( startLocation ) : startLocation ;
133
+
126
134
validateString ( startPath , 'startPath' ) ;
127
135
validateBoolean ( everything , 'everything' ) ;
128
136
137
+ if ( ! path . isAbsolute ( startPath ) ) {
138
+ throw new ERR_INVALID_ARG_VALUE (
139
+ 'startLocation' ,
140
+ startLocation ,
141
+ ArrayPrototypeJoin ( [
142
+ 'must be a fully resolved location. To use a relative location, first wrap with' ,
143
+ '`import.meta.resolve(startLocation)` in ESM' ,
144
+ 'or' ,
145
+ '`path.resolve(__dirname, startLocation) in CJS' ,
146
+ ] , ' ' ) ,
147
+ ) ;
148
+ }
149
+
150
+ if ( ! StringPrototypeEndsWith ( startPath , path . sep ) ) { startPath += path . sep ; }
151
+
129
152
if ( everything ) {
130
153
const result = modulesBinding . getNearestRawParentPackageJSON ( startPath ) ;
131
154
0 commit comments