I am trying to install express into my current "directory".
However node installs this globally and I do not understand, how I can tell node to install it in my current directory.

- 490
- 6
- 19

- 529
- 2
- 6
- 20
-
1Possible duplicate of [npm install module in current directory](https://stackoverflow.com/questions/14032160/npm-install-module-in-current-directory) – Paul Feb 02 '19 at 08:07
2 Answers
I had a similar issue with installing node modules in my project directory, even when I did not specify the "-g" global flag. On Linux any packages I installed when in my current directory would end up getting installed into ~/node_modules (i.e. /home/user/node_modules).
The reason and fix are explained in the thread at npm install module in current directory. Briefly, npm looks for a node_modules subdirectory in the directory where
npm install
was invoked. If not found, npm keeps moving upwards, searching that directory's ancestors till it finds node_modules. Assuming a Linux system, if not found in the uppermost level of the current user's home, i.e. /home/user, it will create node_modules in the current dir, which is the required behaviour. However I already had a ~/node_modules directory, which did not allow this to happen.
The fix is to first run
npm init
in the current directory, which interactively creates a package.json file that tells npm that we're creating a package in that directory, and any dependencies need to be local to the package, hence requiring that node_modules/ and thereby node packages be installed locally.
Following the creation of package,json, install commands run in that directory will install packages such as express locally.

- 379
- 3
- 7