Open stevenmhunt opened 10 months ago
Maybe npm could go a step farther and just refuse to work in the home directory entirely?
I would be hesitant to suggest that, I'm not sure if someone out there has a legitimate reason to do this such as installing scripts in a Docker container for instance. I also think that from a political standpoint it's going to be much easier for the community to agree to adding a warning message than on implementing a behavior change. In the real world situation I just dealt with which prompted me to write this post, I believe that a warning message would have been sufficient to avoid further complications.
Maybe npm could go a step farther and just refuse to work in the home directory entirely?
Preferably not. I do not install packages globally, and I do have a few packages I install in my home directory and I handle setting up my path correctly for that. These are tools I use outside of specific projects. Frankly I think that refusing to do global installs is better than refusing to do home directory installs but both are anti-patterns if you are using the tools in a project where they should be installed as dev deps. But I also dont think that is a change worth the effort from npm.
Motivation ("The Why")
When new and inexperienced users are installing packages with
npm
, they may accidentally open a new terminal to their home directory and then runnpm install
. This can occur if they are installing a package globally and forget to add-g
or it can be the case that they weren't aware of their present working directory. In either case, the result is a~/node_modules
directory which can cause unexpected behaviors in other node projects on their machine.Example
How
Current Behaviour
At this time, there is no warning regarding this situation so when the user performs this action accidentally it occurs silently only to cause problems in the future. Let's consider the following scenario:
The current terminal environment is running NodeJS 18.13.0 and NPM 9.2.0 and the user has now asked NPM to install the
node
version 12 package in their home directory. Realistically this scenario more likely to occur if installing a package which referencesnode
as a dependency. Now, running~/node_modules/.bin/node -v
in this example will output "v12.22.12".What will happen now is if the user makes any reference to
node
ornpm
within the npm-run-script section of anypackage.json
file located within a subdirectory of the home directory (which will typically be the case for most people), NPM will use the other version of NodeJS because./node_modules/.bin
of the current directory as well as all parent directories is added toPATH
by NPM when running script commands:~/projects/some_node_app/package.json
:Because NPM is sensitive to the specific version of NodeJS being used, this specific situation will produce an error because the currently running version of NPM is incompatible with the hidden version of NodeJS:
Throughout this entire scenario NPM has been working as intended and the root cause of this error is the user mis-configuring their environment. However, I believe that since this is detectable situation that has been reported by users it is worth adding a warning message to bring attention to a potentially confusing issue.
Desired Behaviour
It would be ideal to present the user with some sort of warning about installing packages in their home directory so that way they are made aware of the fact that they may be introducing unexpected behaviors in the future.
References