Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

"npm install somepackage" does not warn if working dir has no package.json #3333

Closed
meryn opened this Issue · 9 comments

5 participants

@meryn

Doing npm install somepackage in a directory that has no package.json, will cause npm to install the package to the user's home dir, without any warning about its (imo) surprising behavior.

I think npm should emit a warning before doing this, because the end-result surprised me. It took me a while to realize at first that the relative path that npm showed as resulting install directory for the package (i.e. something like ../../node_modules/somepackage, dependening where your working directory happened to be relative to your home dir) was in fact my home dir.

A warning would also be appropriate because I think this is not "recommended" usage of npm. It certainly makes sense to be using npm without having any package of your own, when your not a node.js developer. But then doing global installs would be more logical, because it's recommended that the global npm "bin" dir is in your path. Compared to global installs, there's not much benefit from installing to ~/node_modules. Also because the npm path prefix can be configured per user in .npmrc.

@luk-

This is fairly clearly explained in the docs:

Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a package.json file, or a node_modules folder. If such a thing is found,
then that is treated as the effective "current directory" for the purpose of running npm commands. (This behavior is inspired by and similar to git's .git-folder seeking
logic when running git commands in a working dir.)

A warning would probably not be appropriate because it might cause confusion that something is wrong.

@meryn
  1. What page is that taken from? I was looking at https://npmjs.org/doc/install.html (edit: found it, https://npmjs.org/doc/folders.html )
  2. There's no mention of a user's home dir there right?

Hmm now I understand behavior somewhat better. I hadn't noticed it before. It is because I had accidentally installed something in my home dir (i.e. into ~/node_modules ). That caused the algorithm to choose my home dir.

I still think it's surprising behavior, and I rather be warned (or prevented from doing this altogether, unless forced or so).

@meryn

If npm install packagename would ask for confirmation before installing something to a directory without a package.json, I wouldn't have "cluttered" my home dir in the first place.

Important question is: How many people rely on local npm installs while they don't have a package.json in that dir? Don't these two in practice always go hand-in-hand together? I have no problem with the functionality being there, but I wonder if it should be the default behavior.

Also, to support installing into a project's "root", when in a subdirectory (say you're in the the yourproject/lib dir), then instead of the closest dir with a node_modules subdir, it could search for the closest dir with a package.json in it.

@mfncooper
Collaborator

Installing dependent packages first makes perfect sense, because then when I use npm init to create my package.json file, all of the dependencies are included automatically.

@meryn

all of the dependencies are included automatically

That's very neat functionality. I didn't know about it before. It seems it isn't documented, really.

https://npmjs.org/doc/init.html says

This will ask you a bunch of questions, and then write a package.json for you.
It attempts to make reasonable guesses about what you want things to be set to, and then writes a package.json file with the options you've selected.
If you already have a package.json file, it'll read that first, and default to the options in there.
It is strictly additive, so it does not delete options from your package.json without a really good reason to do so.

This does make npm install packagename behavior without package.json more understandable.

@domenic domenic closed this
@azat-co

Is there a way to just install modules into a current folder without package.json/node_modules in it? Like param flag or something?

@luk-

If your current directory has either a node_modules directory or a package.json file it will put your modules in it (in the case of the latter, creating a node_modules directory if it does not exist). Otherwise, it will walk up the directory tree until it finds one.

@azat-co

@luk- Is there a way to install modules into a current working folder without either package.json or node_modules in it? Like an option or a flag?

@luk-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.