Why does RequiredModules fail to load local .psm1 files in PowerShell 5.1?

0
0
Asked By MellowPine42 On

I'm building a test module with a manifest named Template.psd1 and a module file in the same directory. The manifest declares several local module files under a Control subfolder as required modules:

RequiredModules = @(
'Control\ControlMessaging.psm1',
'Control\ControlChecklist.psm1',
'Control\ControlPrompt.psm1'
)

The files exist beside the manifest in the Control directory. Importing the manifest works in PowerShell 7.4, but PowerShell 5.1 reports that Control\ControlMessaging.psm1 could not be found in any module directory. Using .\ prefixes produces the same result. I need the module to work in both PowerShell 5.1 and 7.4. What is the correct way to handle these local dependencies?

3 Answers

Answered By QuietHarbor17 On

If these Control files are part of your module rather than external dependencies, RequiredModules probably isn’t the right mechanism. Import them from Template.psm1 with Import-Module using paths based on the module’s location, or structure them as nested modules. Imports performed inside the module run in its module scope, and you can explicitly export anything that should be exposed publicly.

Answered By SilverMaple63 On

RequiredModules is primarily intended for dependencies that are installed as modules and discoverable through PSModulePath, such as separately published modules. For local files bundled with your project, importing them from the main .psm1 is usually more compatible with Windows PowerShell 5.1. Adding .\ to the RequiredModules entries won’t avoid the underlying 5.1 limitation.

Answered By CobaltWren8 On

PowerShell 5.1 has known path-handling problems with RequiredModules. In that version, a local .psm1 path generally isn’t handled correctly as a required module. A .psd1 can work, but it needs to use a fully qualified path. Later PowerShell versions improved this behavior.

MellowPine42 -

That explains the difference between the two versions. Thanks for clarifying!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.