11'use strict' ;
22const {
3- RegExpPrototypeExec,
43 ObjectAssign,
54 ObjectCreate,
65 ObjectPrototypeHasOwnProperty,
76 PromisePrototypeThen,
87 PromiseResolve,
8+ RegExpPrototypeExec,
9+ StringPrototypeSlice,
910} = primordials ;
10- const { extname } = require ( 'path' ) ;
11+ const { basename , extname, relative } = require ( 'path' ) ;
1112const { getOptionValue } = require ( 'internal/options' ) ;
1213const { fetchModule } = require ( 'internal/modules/esm/fetch_module' ) ;
1314const {
@@ -20,7 +21,7 @@ const experimentalNetworkImports =
2021 getOptionValue ( '--experimental-network-imports' ) ;
2122const experimentalSpecifierResolution =
2223 getOptionValue ( '--experimental-specifier-resolution' ) ;
23- const { getPackageType } = require ( 'internal/modules/esm/resolve' ) ;
24+ const { getPackageType, getPackageScopeConfig } = require ( 'internal/modules/esm/resolve' ) ;
2425const { URL , fileURLToPath } = require ( 'internal/url' ) ;
2526const { ERR_UNKNOWN_FILE_EXTENSION } = require ( 'internal/errors' ) . codes ;
2627
@@ -48,7 +49,8 @@ function getDataProtocolModuleFormat(parsed) {
4849 * @returns {string }
4950 */
5051function getFileProtocolModuleFormat ( url , context , ignoreErrors ) {
51- const ext = extname ( url . pathname ) ;
52+ const filepath = fileURLToPath ( url ) ;
53+ const ext = extname ( filepath ) ;
5254 if ( ext === '.js' ) {
5355 return getPackageType ( url ) === 'module' ? 'module' : 'commonjs' ;
5456 }
@@ -59,7 +61,19 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) {
5961 if ( experimentalSpecifierResolution !== 'node' ) {
6062 // Explicit undefined return indicates load hook should rerun format check
6163 if ( ignoreErrors ) return undefined ;
62- throw new ERR_UNKNOWN_FILE_EXTENSION ( ext , fileURLToPath ( url ) ) ;
64+ let suggestion = '' ;
65+ if ( getPackageType ( url ) === 'module' && ext === '' ) {
66+ const config = getPackageScopeConfig ( url ) ;
67+ const fileBasename = basename ( filepath ) ;
68+ const relativePath = StringPrototypeSlice ( relative ( config . pjsonPath , filepath ) , 1 ) ;
69+ suggestion = 'Loading extensionless files is not supported inside of ' +
70+ '"type":"module" package.json contexts. The package.json file ' +
71+ `${ config . pjsonPath } caused this "type":"module" context. Try ` +
72+ `changing ${ filepath } to have a file extension. Note the "bin" ` +
73+ 'field of package.json can point to a file with an extension, for example ' +
74+ `{"type":"module","bin":{"${ fileBasename } ":"${ relativePath } .js"}}` ;
75+ }
76+ throw new ERR_UNKNOWN_FILE_EXTENSION ( ext , filepath , suggestion ) ;
6377 }
6478
6579 return getLegacyExtensionFormat ( ext ) ?? null ;
0 commit comments