3

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
  • 161
  • 7
  • 1
    I don't think this has a technological solution, I feel it is more behavourial. Can you change your command prompt to show the current directory? – Burgi Nov 25 '19 at 09:17
  • Yeah, I need to slow down a bit. – zer09 Nov 26 '19 at 01:59

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
  • 161
  • 7
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
  • 11,884
  • 16
  • 55
  • 68
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