A Computer Polyglot, CLI + WEB ♥'r.

Npm --save to Create package.json

| Comments

Till issue npm#3576 is fixed we could use the below is our shell's rc to do the job:

function npms(){
    [[ -f ./package.json ]] ||
    echo {} > package.json  &&
    npm install --save $*

Quoting timoxley for the context:

"adds to package.json in current working directory, and creates if one is not found" is IMO conceptually simpler than the current situation.

Currently, npm install --save adds the dependency to the nearest package.json, searching up the directory tree. I doubt this is used very often, and it likely leads to more dependencies accidentally being added to the wrong package.json than the correct package.json.

npm also uses the realdir when handling symlinks, so if you're calling npm install --save from inside a symlinked dir, it'll find and add the dep the package.json in the parent real directory, rather than the symlinked one, so you now also need to worry about whether the directory you're in is a symlink or not. Maybe this feature is useful, maybe it's complicated.

The suggestion of calling npm init on missing package.json makes a lot of sense to me if we simplified the install logic to always just look in cwd.

Changing this behaviour could break a few builds, and there's also some consistency with the "find nearest package.json" logic across other npm commands, so it'd likely need to be changed there as well.