diff --git a/configure.py b/configure.py index e98801f2279f6b..eb60e3ab99ec63 100755 --- a/configure.py +++ b/configure.py @@ -641,6 +641,12 @@ default=False, help='node will load builtin modules from disk instead of from binary') +parser.add_option('--default-package-type', + action='store', + dest='node_default_package_type', + default=False, + help='node will treat files outside of package boundaries as this type') + # Create compile_commands.json in out/Debug and out/Release. parser.add_option('-C', action='store_true', @@ -1185,6 +1191,11 @@ def configure_node(o): print('Warning! Loading builtin modules from disk is for development') o['variables']['node_builtin_modules_path'] = options.node_builtin_modules_path + if options.node_default_package_type: + if not ( options.node_default_package_type in ['module', 'commonjs'] ): + raise Exception('--default-package-type must be either "module" or "commonjs"'); + o['variables']['node_default_package_type'] = options.node_default_package_type + def configure_napi(output): version = getnapibuildversion.get_napi_version() output['variables']['napi_build_version'] = version diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 616b2cf52309ea..951a12f5e83c6b 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -10,6 +10,7 @@ const experimentalWasmModules = getOptionValue('--experimental-wasm-modules'); const { getPackageType } = require('internal/modules/esm/resolve'); const { URL, fileURLToPath } = require('internal/url'); const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes; +const { defaultPackageType } = internalBinding('config'); const extensionFormatMap = { '__proto__': null, @@ -51,7 +52,8 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) { const ext = extname(parsed.pathname); let format; if (ext === '.js') { - format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs'; + const type = getPackageType(parsed.href); + format = type !== 'none' ? type : defaultPackageType; } else { format = extensionFormatMap[ext]; } diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js index 5f8b1f53d33768..f3b6cad32d0285 100644 --- a/lib/internal/modules/run_main.js +++ b/lib/internal/modules/run_main.js @@ -4,6 +4,7 @@ const CJSLoader = require('internal/modules/cjs/loader'); const { Module, toRealPath, readPackageScope } = CJSLoader; const { getOptionValue } = require('internal/options'); const path = require('path'); +const { defaultPackageType } = internalBinding('config'); function resolveMainPath(main) { // Note extension resolution for the main entry point can be deprecated in a @@ -33,8 +34,11 @@ function shouldUseESMLoader(mainPath) { return true; if (!mainPath || mainPath.endsWith('.cjs')) return false; - const pkg = readPackageScope(mainPath); - return pkg && pkg.data.type === 'module'; + const pkgType = readPackageScope(mainPath)?.data?.type ?? 'none'; + return pkgType === 'module' || ( + pkgType === 'none' && + defaultPackageType === 'module' + ); } function runMainESM(mainPath) { diff --git a/node.gyp b/node.gyp index c8e7387c2a8cf1..a56770971b300b 100644 --- a/node.gyp +++ b/node.gyp @@ -26,6 +26,7 @@ 'node_lib_target_name%': 'libnode', 'node_intermediate_lib_type%': 'static_library', 'node_builtin_modules_path%': '', + 'node_default_package_type%': '', 'library_files': [ 'lib/internal/bootstrap/environment.js', 'lib/internal/bootstrap/loaders.js', @@ -748,6 +749,9 @@ [ 'node_builtin_modules_path!=""', { 'defines': [ 'NODE_BUILTIN_MODULES_PATH="<(node_builtin_modules_path)"' ] }], + [ 'node_default_package_type!=""', { + 'defines': [ 'NODE_DEFAULT_PACKAGE_TYPE="<(node_default_package_type)"' ] + }], [ 'node_shared=="true"', { 'sources': [ 'src/node_snapshot_stub.cc', diff --git a/src/node_config.cc b/src/node_config.cc index 6ee3164a134fe8..06e94f87a05d02 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -85,6 +85,16 @@ static void Initialize(Local target, READONLY_TRUE_PROPERTY(target, "hasDtrace"); #endif +#if defined NODE_DEFAULT_PACKAGE_TYPE + READONLY_PROPERTY(target, + "defaultPackageType", + FIXED_ONE_BYTE_STRING(isolate, NODE_DEFAULT_PACKAGE_TYPE)); +#else + READONLY_PROPERTY(target, + "defaultPackageType", + FIXED_ONE_BYTE_STRING(isolate, "commonjs")); +#endif + READONLY_PROPERTY(target, "hasCachedBuiltins", v8::Boolean::New(isolate, native_module::has_code_cache)); } // InitConfig