From 1e429816704522ff7e1f5e7914568d42d4e50110 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Fri, 22 Nov 2019 14:29:30 +0800 Subject: [PATCH] module: fix specifier resolution algorithm Fixes: https://github.com/nodejs/node/issues/30520 --- lib/internal/modules/run_main.js | 4 ++++ test/es-module/test-esm-specifiers.mjs | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js index 1061727c78269c..5f8b1f53d33768 100644 --- a/lib/internal/modules/run_main.js +++ b/lib/internal/modules/run_main.js @@ -24,6 +24,10 @@ function shouldUseESMLoader(mainPath) { const userLoader = getOptionValue('--experimental-loader'); if (userLoader) return true; + const esModuleSpecifierResolution = + getOptionValue('--es-module-specifier-resolution'); + if (esModuleSpecifierResolution === 'node') + return true; // Determine the module format of the main if (mainPath && mainPath.endsWith('.mjs')) return true; diff --git a/test/es-module/test-esm-specifiers.mjs b/test/es-module/test-esm-specifiers.mjs index 3e7bc181962f4f..fdf9e5b25ecea4 100644 --- a/test/es-module/test-esm-specifiers.mjs +++ b/test/es-module/test-esm-specifiers.mjs @@ -1,6 +1,9 @@ // Flags: --es-module-specifier-resolution=node import { mustNotCall } from '../common/index.mjs'; import assert from 'assert'; +import path from 'path'; +import { spawn } from 'child_process'; +import { fileURLToPath } from 'url'; // commonJS index.js import commonjs from '../fixtures/es-module-specifiers/package-type-commonjs'; @@ -33,3 +36,22 @@ async function main() { } main().catch(mustNotCall); + +// Test path from command line arguments +[ + 'package-type-commonjs', + 'package-type-module', + '/', + '/index', +].forEach((item) => { + const modulePath = path.join( + fileURLToPath(import.meta.url), + '../../fixtures/es-module-specifiers', + item, + ); + spawn(process.execPath, + ['--es-module-specifier-resolution=node', modulePath], + { stdio: 'inherit' }).on('exit', (code) => { + assert.strictEqual(code, 0); + }); +});