Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

splatteredbits
Copy link
Contributor

@splatteredbits splatteredbits commented Jul 29, 2025

Fixes issue #8468: npm.ps1 and npx.ps1 fail on Windows PowerShell 5.1 when run in strict mode.

References

@splatteredbits splatteredbits requested a review from a team as a code owner July 29, 2025 18:32
@splatteredbits splatteredbits changed the title Fix 8468 Fixes 8468: "The property 'Statement" cannot be found on this object" error when npm.ps1 run in strict mode. Jul 29, 2025
@splatteredbits splatteredbits changed the title Fixes 8468: "The property 'Statement" cannot be found on this object" error when npm.ps1 run in strict mode. Fixes #8468: "The property 'Statement" cannot be found on this object" error when npm.ps1 run in strict mode. Jul 29, 2025
@alexsch01
Copy link
Contributor

alexsch01 commented Aug 1, 2025

@splatteredbits

can we change

if (($MyInvocation | Get-Member -Name 'Statement') -and $MyInvocation.Statement) {

to

if ($MyInvocation | Get-Member -Name 'Statement') {

Otherwise, this PR looks good to me @wraithgar @mbtools

@alexsch01
Copy link
Contributor

alexsch01 commented Aug 1, 2025

Don't know how many people will run into this issue but it is in NodeJS 22.18.0+ / 24.3.0+
Appreciate the fix since this was my code that didn't account for this

@splatteredbits
Copy link
Contributor Author

@splatteredbits

can we change

if (($MyInvocation | Get-Member -Name 'Statement') -and $MyInvocation.Statement) {

to

if ($MyInvocation | Get-Member -Name 'Statement') {

Otherwise, this PR looks good to me @wraithgar @mbtools

I think we should keep both. The first checks that the Statement member even exists (which it does only on PowerShell 7+), the second condition checks if it has a value. If strict mode is off, if ($MyInvocation.Statement) does both for you, but with strict mode on, you're responsible to check that members exist. But they are checking slightly different things.

@alexsch01
Copy link
Contributor

alexsch01 commented Aug 1, 2025

@splatteredbits Ok I understand the difference now

@mbtools
Copy link
Contributor

mbtools commented Aug 1, 2025

Lgtm

@wraithgar wraithgar changed the title Fixes #8468: "The property 'Statement" cannot be found on this object" error when npm.ps1 run in strict mode. fix: ps1 scripts in powershell 5.1 Aug 4, 2025
@wraithgar wraithgar merged commit 5d82d0b into npm:latest Aug 4, 2025
36 of 42 checks passed
@github-actions github-actions bot mentioned this pull request Aug 4, 2025
alexsch01 pushed a commit to alexsch01/cli that referenced this pull request Sep 5, 2025
Fixes issue npm#8468: npm.ps1 and npx.ps1 fail on Windows PowerShell 5.1 when run in strict mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants