bus1hero
bus1hero

Reputation: 309

How to avoid repeating the exported functions in the manifest(*.psd1) and the module file(*.psm1)?

I'm building a PowerShell module. It contains several public functions as well as a number of private utility functions. I want for the users of the module to know only about the public functions.

I've created Project.psm1.

. "${PSScriptRoot}\public\Foo.ps1"

Export-ModuleMember -Function Foo

Because I intend to publish the module to the PowerShell Gallery, I also need to have a manifest file. I've created a script that will generate a manifest for the project.

New-ModuleManifest `
    -Guid 'd1f545c4-49db-4fd8-bc14-53df36387757' `
    -Path path\to\project `
    -RootModule "Project.psm1" `
    -FunctionsToExport 'Foo'

I don't like that I have to specify Foo in 2 places. If I rename or delete Foo, or add a new function to export, I will have to change two files and eventually, I will forget about one of them.

Is there a way to have all the functions to export specified in a single place?

Upvotes: 1

Views: 332

Answers (1)

mklement0
mklement0

Reputation: 439587

  • The exports explicitly specified in a module manifest (.psd1 file) additionally constrain the implicit or explicit exports of a script module (.psm1 file).

  • Implicitly - in the absence of an Export-ModuleMember call - a script module exports all its (a) functions and (b) aliases, but not its variables.

Given that exporting variables from modules is best avoided,[1] there is usually no reason to use Export-ModuleMember in a script module file.

Thus, controlling what functions (and aliases) your module exports need only be maintained in the FunctionsToExport (AliasesToExport) entry of the module manifest.


[1] Not only would exporting variables increase the risk of name collisions, it also presents a discoverability problem; generally, users expect modules only to export commands, which can be discovered with Get-Command and help for which can be requested with Get-Help / -?.

Upvotes: 1

Related Questions