title: Getting "InvalidOperation: Cannot run a document in the middle of a pipeline:" error when building powershell module in Visual Studio Code
body: <p><strong>Problem Description</strong></p>
  <p>When I select <em>Run Without Debugging</em> in Visual Studio Code 1.58.2 on the module SampleModule.psm1</p>
  <pre><code>function ConvertTo-PascalCase([String []] $words) {
      [String] $pascalCaseString = [String]::Empty
  
      foreach ($word in $words) {
          $pascalCaseString = $pascalCaseString + ($word.Substring(0,1).ToUpper() + $word.Substring(1))
      }
      $pascalCaseString = ($pascalCaseString.TrimEnd(',')).Trim()
      
      return $pascalCaseString
  }
  
  Export-ModuleMember -Function Convert-ToPascalCase
  </code></pre>
  <p>I keep getting the error message:</p>
  <blockquote>
  <p>InvalidOperation: Cannot run a document in the middle of a pipeline</p>
  </blockquote>
  <p>The screenshot shows it in more detail.  I have got this same error message whenever I build any module</p>
  <p><a href="https://i.stack.imgur.com/TWhpC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TWhpC.png" alt="vscode error message for running *.psm1 file" /></a></p>
  <p><strong>Things I Tried That Did Not Work</strong>:</p>
  <ol>
  <li>Checking my PowerShell extensions in VSCode and their settings</li>
  <li>Using different PowerShell versions in VSCode</li>
  <li>Including a manifest makes no difference</li>
  <li>Researching the error message for more details about it's meaning in this specific case</li>
  <li>Checked the configuration of my VSCode Terminal environment</li>
  </ol>
  <p><strong>How I Know It's an Issue With Running PSM1 Files</strong></p>
  <p>If I convert the .psm1 module file back into a plain old Powershell script (*.ps1) script and remove all the <em>Export-Module member</em> commands the script runs just fine.</p>
  <p><strong>My Work Around</strong></p>
  <p>To execute the module in VSCode I have to dotsource to successfully import the module:</p>
  <pre><code>PS C:\Users\griot\SampleModule&gt; .\SampleModule.psm1
  
  PS C:\Users\griot\SampleModule&gt; Import-Module .\SampleModule.psm1
  
  PS C:\Users\griot\SampleModule&gt;
  </code></pre>
  <p><strong>My Questions</strong></p>
  <ol>
  <li><p>Any ideas why I am getting the error message?</p>
  </li>
  <li><p>What is the recommended way to run and debug PowerShell modules in VSCode?</p>
  </li>
  </ol>