Why does RequiredModules fail to load local .psm1 files in PowerShell 5.1?
Asked by
MellowPine42
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?
Answers
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.
Reply to this answer
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.
Reply to this answer
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.
Reply to this answer
Add your answer
Answers are reviewed before publication.
MellowPine42: That explains the difference between the two versions. Thanks for clarifying!