1
- // copied from https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js
1
+ // Copied from several files in node's source code.
2
+ // https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/modules/cjs/loader.js
3
+ // Each function and variable below must have a comment linking to the source in node's github repo.
4
+
2
5
const path = require ( 'path' ) ;
3
6
const fs = require ( 'fs' ) ;
4
7
5
8
module . exports . assertScriptCanLoadAsCJSImpl = assertScriptCanLoadAsCJSImpl ;
6
9
7
10
// copied from Module._extensions['.js']
11
+ // https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/modules/cjs/loader.js#L1211-L1217
8
12
function assertScriptCanLoadAsCJSImpl ( filename ) {
9
13
const pkg = readPackageScope ( filename ) ;
10
14
// Function require shouldn't be used in ES modules.
@@ -15,6 +19,7 @@ function assertScriptCanLoadAsCJSImpl(filename) {
15
19
}
16
20
}
17
21
22
+ // Copied from https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/modules/cjs/loader.js#L285-L301
18
23
function readPackageScope ( checkPath ) {
19
24
const rootSeparatorIndex = checkPath . indexOf ( path . sep ) ;
20
25
let separatorIndex ;
@@ -33,8 +38,10 @@ function readPackageScope(checkPath) {
33
38
return false ;
34
39
}
35
40
41
+ // Copied from https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/modules/cjs/loader.js#L249
36
42
const packageJsonCache = new Map ( ) ;
37
43
44
+ // Copied from https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/modules/cjs/loader.js#L251-L283
38
45
function readPackage ( requestPath ) {
39
46
const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
40
47
@@ -70,6 +77,8 @@ function readPackage(requestPath) {
70
77
}
71
78
}
72
79
80
+ // In node's core, this is implemented in C
81
+ // https://github.com/nodejs/node/blob/e9f293750760d59243020d0376edf242c9a26b67/src/node_file.cc#L845-L939
73
82
function internalModuleReadJSON ( path ) {
74
83
try {
75
84
return fs . readFileSync ( path , 'utf8' )
@@ -79,10 +88,18 @@ function internalModuleReadJSON(path) {
79
88
}
80
89
}
81
90
91
+ // Native ERR_REQUIRE_ESM Error is declared here:
92
+ // https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/errors.js#L1294-L1313
93
+ // Error class factory is implemented here:
94
+ // function E: https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/errors.js#L323-L341
95
+ // function makeNodeErrorWithCode: https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/errors.js#L251-L278
96
+ // The code below should create an error that matches the native error as closely as possible.
97
+ // Third-party libraries which attempt to catch the native ERR_REQUIRE_ESM should recognize our imitation error.
82
98
function createErrRequireEsm ( filename , parentPath , packageJsonPath ) {
83
- // Attempt to create an error object that matches node's native error close enough
84
99
const code = 'ERR_REQUIRE_ESM'
85
100
const err = new Error ( getMessage ( filename , parentPath , packageJsonPath ) )
101
+ // Set `name` to be used in stack trace, generate stack trace with that name baked in, then re-declare the `name` field.
102
+ // This trick is copied from node's source.
86
103
err . name = `Error [${ code } ]`
87
104
err . stack
88
105
Object . defineProperty ( err , 'name' , {
@@ -94,7 +111,8 @@ function createErrRequireEsm(filename, parentPath, packageJsonPath) {
94
111
err . code = code
95
112
return err
96
113
97
- // copy-pasted from https://github.com/nodejs/node/blob/master/lib/internal/errors.js#L1294-L1311
114
+ // Copy-pasted from https://github.com/nodejs/node/blob/b533fb3508009e5f567cc776daba8fbf665386a6/lib/internal/errors.js#L1293-L1311
115
+ // so that our error message is identical to the native message.
98
116
function getMessage ( filename , parentPath = null , packageJsonPath = null ) {
99
117
const ext = path . extname ( filename )
100
118
let msg = `Must use import to load ES Module: ${ filename } ` ;
0 commit comments