I have the following directory structure:
└─ScriptModuleRepositoryTemplate
│ ScriptModuleRepositoryTemplate.psd1
│ ScriptModuleRepositoryTemplate.psm1
│ ScriptModuleRepositoryTemplate.Tests.ps1
At the top of the Pester ScriptModuleRepositoryTemplate.Tests.ps1
file I have this statement to import the module:
using module './ScriptModuleRepositoryTemplate.psm1'
The module is imported and the Pester tests run fine in Pwsh 7, but on Windows PowerShell 5.1 it throws the error:
System.IO.FileNotFoundException: The specified module 'D:\a\PowerShell.ScriptModuleRepositoryTemplate\PowerShell.ScriptModuleRepositoryTemplate\ScriptModuleRepositoryTemplate.psm1' was not loaded because no valid module file was found in any module directory.
If I instead use Import-Module
like this:
Import-Module -Name "$PSScriptRoot/ScriptModuleRepositoryTemplate.psm1"
it works in both PowerShell 5.1 and 7, but of course Import-Module
does not import classes and enums, so I need to use using module
.
Both Pwsh and Windows PowerShell are using Pester v5.5.0, so I don't think the issue is with Pester, but with Windows PowerShell itself.
I can reproduce the issue on my local machine by running Invoke-Pester .
, as well as in GitHub Actions (see this workflow run in the open source project).
Does anyone have any ideas on how I can get using module './PathToModuleFile.psm1'
to work properly?
Ugggghh, it turns out the problem was using the forward slash /
in the module file path 🤦♂️. It seems the forward slash works fine with Import-Module
in Windows PowerShell, but not with using module
.
Once I changed:
using module './ScriptModuleRepositoryTemplate.psm1'
to:
using module '.\ScriptModuleRepositoryTemplate.psm1'
then it worked fine in Windows PowerShell 5.1 and Pwsh 7.