4

Because sometimes with my dumb mind, I installed npm packages for a project and realized after that I am still on my home directory. Forgot to change directory.

Is there way to prevent it or will stop and show an error that I am still on my home directory?

Edit

I am not installing global package.

zer09
  • 191

3 Answers3

2

As Burgi said on the comment, there is no solution for this.

This answer is just a workaround.

What I came up with is to add node_modules and package-lock.json to my home directory with a root permission. So that if I mistakenly install a package there, It will just give me an EACCESS error preventing the installation to proceed.

zer09
  • 191
1

You can add a package.json file to your home dir but put a syntax error in it. Mine says "Wrong directory" which fails to parse at JSON. That seems to trip up npm and yarn before it starts installing.

mpen
  • 12,441
0

You could alias npm to a script that checks your current directory

#!/bin/bash
if [ "$(pwd)" = "$HOME" ] && [ "$1" != "version" ]; then
  echo Don\'t do that here!
else
  npm $@
fi