File tree 2 files changed +17
-1
lines changed
2 files changed +17
-1
lines changed Original file line number Diff line number Diff line change 1
1
const nativeModule = require ( 'module' )
2
2
const path = require ( 'path' )
3
+ const fs = require ( 'fs' )
3
4
4
5
function createRequire ( filename ) {
6
+ // If filename is dir, createRequire goes with parent directory, so we need fakepath
7
+ if ( isDir ( filename ) ) {
8
+ filename = path . join ( filename , 'index.js' )
9
+ }
10
+
5
11
// Added in Node v12.2.0
6
12
if ( nativeModule . createRequire ) {
7
13
return nativeModule . createRequire ( filename )
@@ -25,4 +31,14 @@ function _createRequire (filename) {
25
31
return mod . exports
26
32
}
27
33
34
+ function isDir ( path ) {
35
+ try {
36
+ const stat = fs . lstatSync ( path )
37
+ return stat . isDirectory ( )
38
+ } catch ( e ) {
39
+ // lstatSync throws an error if path doesn't exist
40
+ return false
41
+ }
42
+ }
43
+
28
44
module . exports = createRequire
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ const path = require('path')
2
2
const t = require ( 'tap' )
3
3
const createRequire = require ( '../create-require' )
4
4
5
- const _require = createRequire ( path . join ( __dirname , 'some/index.js ' ) )
5
+ const _require = createRequire ( path . join ( __dirname , 'some' ) )
6
6
const e = _require ( './file' )
7
7
t . equal ( e , 'someFile' )
8
8
t . end ( )
You can’t perform that action at this time.
0 commit comments