Skip to content

[BUG] npx does not preserve PATH environment variable correctly. #8648

@ioquatix

Description

@ioquatix

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

npx appears to be inserting /usr/bin ahead of all other path entries, causing problems for version managers that depend on adding specific entries before more general system entries:

> npx --no-install bash -c "env | grep PATH"
GEM_PATH=/home/samuel/.gem/ruby/3.4.6:/home/samuel/.rubies/ruby-3.4.6/lib/ruby/gems/3.4.0
PATH=/usr/bin:/home/samuel/Developer/socketry/lively/examples/game-audio/node_modules/.bin:/home/samuel/Developer/socketry/lively/examples/node_modules/.bin:/home/samuel/Developer/socketry/lively/node_modules/.bin:/home/samuel/Developer/socketry/node_modules/.bin:/home/samuel/Developer/node_modules/.bin:/home/samuel/node_modules/.bin:/home/node_modules/.bin:/node_modules/.bin:/usr/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/node-gyp-bin:/home/samuel/.gem/ruby/3.4.6/bin:/home/samuel/.rubies/ruby-3.4.6/lib/ruby/gems/3.4.0/bin:/home/samuel/.rubies/ruby-3.4.6/bin:/home/samuel/Developer/jruby/jruby/bin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

> bash -c "env | grep PATH"
GEM_PATH=/home/samuel/.gem/ruby/3.4.6:/home/samuel/.rubies/ruby-3.4.6/lib/ruby/gems/3.4.0
PATH=/home/samuel/.gem/ruby/3.4.6/bin:/home/samuel/.rubies/ruby-3.4.6/lib/ruby/gems/3.4.0/bin:/home/samuel/.rubies/ruby-3.4.6/bin:/home/samuel/Developer/jruby/jruby/bin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

Note that /usr/bin is inserted at the start of PATH when running under npx. But it's already listed later on.

Because of that, ruby is resolved differently:

> npx --no-install bash -c "which ruby"
/usr/bin/ruby

> bash -c "which ruby"
/home/samuel/.rubies/ruby-3.4.6/bin/ruby

Expected Behavior

npx should not add system paths to the PATH environment variable.

Steps To Reproduce

See the given commands.

Environment

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