Skip to content

[BUG] "The property 'Statement" cannot be found on this object" error when npm.ps1 run in strict mode. #8468

@splatteredbits

Description

@splatteredbits

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

If PowerShell's strict mode is turned on, npm.ps1 fails. Steps to reproduce:

  1. Open Windows PowerShell 5.1. Make sure npm.ps1 is in your path, i.e. that Get-Command npm.ps1 returns a value.
  2. Run Set-StrictMode -Version 'Latest' ; npm.ps1 version.
  3. Note the errors about missing properties: "ERROR: The property 'Statement' cannot be found on this object. Verify that the property exists."

Update npm.ps1 to support strict mode. It must check if properties exist before using them. The Statement property on $MyInvocation exists in PowerShell 7, but not Windows PowerShell 5.1.

Expected Behavior

I expect no errors and npm.ps1 to not fail when run in strict mode. I recommend adding Set-StrictMode -Version 'Latest' in npm.ps1.

Steps To Reproduce

  1. Open Windows PowerShell 5.1. Make sure npm.ps1 is in your path, i.e. that Get-Command npm.ps1 returns a value.
  2. Run Set-StrictMode -Version 'Latest' ; npm.ps1 version.
  3. Note the errors about missing properties: "ERROR: The property 'Statement' cannot be found on this object. Verify that the property exists."

Environment

  • npm: 11.4.2
  • Node.js: v.24.4.1
  • OS Name: Windows 10
  • System Model Name: Lenovo ThinkPad P15v Gen 3
  • npm config:
; "user" config from C:\Users\********\.npmrc

@********:registry = "https://********/"
registry = "https://********"

; node bin location = C:\Build\********\.node\node.exe
; node version = v24.4.1
; npm local prefix = C:\********
; npm version = 11.4.2
; cwd = C:\********
; HOME = C:\Users\********
; Run `npm config ls -l` to show all defaults.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingNeeds Triageneeds review for next steps

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions