Merge pull request #5465 from yuan-cloud/fix/registry-import-exports-subpath

registry: fix importModule base dir for exports subpaths
pull/5468/head
Nick O'Leary 2026-01-26 10:32:40 +00:00 committed by GitHub
commit bf1f7539b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View File

@ -122,7 +122,7 @@ function importModule(module) {
throw e;
}
const externalModuleDir = getInstallDir();
const moduleDir = path.join(externalModuleDir,"node_modules",module);
const moduleDir = path.join(externalModuleDir,"node_modules",parsedModule.module);
// To handle both CJS and ESM we need to resolve the module to the
// specific file that is loaded when the module is required/imported
// As this won't be on the natural module search path, we use createRequire

View File

@ -344,6 +344,36 @@ describe("externalModules api", function() {
should.exist(result);
should.exist(result.existsSync);
})
it("imports external modules using export aliasing", async function() {
const externalModulesDir = path.join(homeDir,"externalModules");
await fs.ensureDir(externalModulesDir);
externalModules.init({userDir: externalModulesDir, get:()=>{}, set:()=>{}});
await fs.writeFile(path.join(externalModulesDir,"package.json"),`{
"name": "Node-RED-External-Modules",
"description": "These modules are automatically installed by Node-RED to use in Function nodes.",
"version": "1.0.0",
"private": true,
"dependencies": {"fake-pkg":"1.0.0"}
}`)
const packageDir = path.join(externalModulesDir,"node_modules","fake-pkg");
await fs.ensureDir(path.join(packageDir,"dist"));
await fs.writeFile(path.join(packageDir,"package.json"),`{
"name": "fake-pkg",
"version": "1.0.0",
"exports": {
"./M.js": "./dist/M.js"
}
}`)
await fs.writeFile(path.join(packageDir,"dist","M.js"),"module.exports = 42;\n");
await externalModules.checkFlowDependencies([]);
const importedValue = await externalModules.import("fake-pkg/M.js")
const normalizedValue = importedValue && importedValue.default !== undefined ? importedValue.default : importedValue;
normalizedValue.should.equal(42);
})
it("rejects unknown modules", async function() {
externalModules.init({userDir: homeDir, get:()=>{}, set:()=>{}});
try {