UNPKG

3.88 kBJavaScriptView Raw
1'use strict';
2
3const path = require('path');
4
5exports.codes = {
6 ERR_INPUT_TYPE_NOT_ALLOWED: createErrorCtor(joinArgs('ERR_INPUT_TYPE_NOT_ALLOWED')),
7 ERR_INVALID_ARG_VALUE: createErrorCtor(joinArgs('ERR_INVALID_ARG_VALUE')),
8 ERR_INVALID_MODULE_SPECIFIER: createErrorCtor(joinArgs('ERR_INVALID_MODULE_SPECIFIER')),
9 ERR_INVALID_PACKAGE_CONFIG: createErrorCtor(joinArgs('ERR_INVALID_PACKAGE_CONFIG')),
10 ERR_INVALID_PACKAGE_TARGET: createErrorCtor(joinArgs('ERR_INVALID_PACKAGE_TARGET')),
11 ERR_MANIFEST_DEPENDENCY_MISSING: createErrorCtor(joinArgs('ERR_MANIFEST_DEPENDENCY_MISSING')),
12 ERR_MODULE_NOT_FOUND: createErrorCtor((path, base, type = 'package') => {
13 return `Cannot find ${type} '${path}' imported from ${base}`
14 }),
15 ERR_PACKAGE_IMPORT_NOT_DEFINED: createErrorCtor(joinArgs('ERR_PACKAGE_IMPORT_NOT_DEFINED')),
16 ERR_PACKAGE_PATH_NOT_EXPORTED: createErrorCtor(joinArgs('ERR_PACKAGE_PATH_NOT_EXPORTED')),
17 ERR_UNSUPPORTED_DIR_IMPORT: createErrorCtor(joinArgs('ERR_UNSUPPORTED_DIR_IMPORT')),
18 ERR_UNSUPPORTED_ESM_URL_SCHEME: createErrorCtor(joinArgs('ERR_UNSUPPORTED_ESM_URL_SCHEME')),
19 ERR_UNKNOWN_FILE_EXTENSION: createErrorCtor(joinArgs('ERR_UNKNOWN_FILE_EXTENSION')),
20}
21
22function joinArgs(name) {
23 return (...args) => {
24 return [name, ...args].join(' ')
25 }
26}
27
28function createErrorCtor(errorMessageCreator) {
29 return class CustomError extends Error {
30 constructor(...args) {
31 super(errorMessageCreator(...args))
32 }
33 }
34}
35exports.createErrRequireEsm = createErrRequireEsm;
36
37// Native ERR_REQUIRE_ESM Error is declared here:
38// https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/errors.js#L1294-L1313
39// Error class factory is implemented here:
40// function E: https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/errors.js#L323-L341
41// function makeNodeErrorWithCode: https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/errors.js#L251-L278
42// The code below should create an error that matches the native error as closely as possible.
43// Third-party libraries which attempt to catch the native ERR_REQUIRE_ESM should recognize our imitation error.
44function createErrRequireEsm(filename, parentPath, packageJsonPath) {
45 const code = 'ERR_REQUIRE_ESM'
46 const err = new Error(getErrRequireEsmMessage(filename, parentPath, packageJsonPath))
47 // Set `name` to be used in stack trace, generate stack trace with that name baked in, then re-declare the `name` field.
48 // This trick is copied from node's source.
49 err.name = `Error [${ code }]`
50 err.stack
51 Object.defineProperty(err, 'name', {
52 value: 'Error',
53 enumerable: false,
54 writable: true,
55 configurable: true
56 })
57 err.code = code
58 return err
59}
60
61// Copy-pasted from https://github.com/nodejs/node/blob/b533fb3508009e5f567cc776daba8fbf665386a6/lib/internal/errors.js#L1293-L1311
62// so that our error message is identical to the native message.
63function getErrRequireEsmMessage(filename, parentPath = null, packageJsonPath = null) {
64 const ext = path.extname(filename)
65 let msg = `Must use import to load ES Module: ${filename}`;
66 if (parentPath && packageJsonPath) {
67 const path = require('path');
68 const basename = path.basename(filename) === path.basename(parentPath) ?
69 filename : path.basename(filename);
70 msg +=
71 '\nrequire() of ES modules is not supported.\nrequire() of ' +
72 `${filename} ${parentPath ? `from ${parentPath} ` : ''}` +
73 `is an ES module file as it is a ${ext} file whose nearest parent ` +
74 `package.json contains "type": "module" which defines all ${ext} ` +
75 'files in that package scope as ES modules.\nInstead ' +
76 'change the requiring code to use ' +
77 'import(), or remove "type": "module" from ' +
78 `${packageJsonPath}.\n`;
79 return msg;
80 }
81 return msg;
82}