41

When I run:

npm install my-app

The app is installed into node_modules/my-app/...

I also tried

npm install -g my-app

But that doesn't work either.

How can I install the module straight into the current directory?

meetar
  • 7,443
  • 8
  • 42
  • 73

8 Answers8

60

npm install installs packages either locally or globally:

  • Locally: npm looks for an existing folder called node_modules in the current directory and creates a folder for each package you install in that folder. If it can't find an existing node_modules folder here, it then looks through the current directory's ancestors until it finds one. If it can't find one, it creates one in the current directory.
  • Globally: if you use the -g (global) option, the package is installed in a global location. This location varies per Linux distribution, but /usr/local/lib/node_modules/packagename is one example. CentOS7 uses /usr/lib/node_modules/packagename.

You should only use -g when the package is something you'd want to use as a command.

Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed.

In general, the rule of thumb is:

  1. If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
  2. If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.

npm will not install a package's files directly into the current directory.

However, this is actually a good thing. It keeps dependencies' files separate from your app, and Node automatically searches the node_modules folder when you require something.

rrauenza
  • 6,285
  • 4
  • 32
  • 57
josh3736
  • 139,160
  • 33
  • 216
  • 263
  • 34
    Your first bullet is only slightly true. If there is no `./node_modules/` folder in the current directory, then npm will search the ancestor directories until it finds one with `node_modules/` in it. If it finds one, let's say in the grandparent directory, then `npm install packagename` will install `packagename` in `../../node_modules/`. However if it searches up the file tree until it reaches the root and still does not find any `node_modules/` folders, then you are correct, npm will automatically generate that folder in the current directory and install the package there. – chharvey Mar 01 '15 at 02:18
  • 2
    Thanks. It was doing the ancestor directory thing for me, I just created a folder locally and voila. – Anthony Nov 06 '15 at 19:59
  • Thanks! That parent folder traversing was my issue. – Fr0sT Jan 17 '17 at 11:51
  • I am creating Node app and wanted to keep Server related packages separate from Client related packages. So when I go to folder where I need my client packages, npm insall --save would go to parent node_module and add in that location. You can instead just do npm init ; which will create package.json in current folder and then running npm install will create in in current folder. Thanks @achedeuzot – Mahesh Mar 29 '17 at 22:12
  • 2
    @chharvey I made an edit -- does that clarify it well enough? – rrauenza Feb 27 '18 at 19:44
  • just checked - it is not working, at least in 6.4.1 – Dmitry Gusarov Mar 15 '19 at 19:47
  • A feature request has been made for that purpose: https://github.com/npm/cli/issues/1578 – João Pimentel Ferreira Jul 30 '20 at 10:41
  • `npm will automatically generate that folder in the current directory and install the package there.` v3.10 that seems not to be the case. Should I just create the directory? – Otheus May 20 '21 at 15:04
39

As @dalu said, if you want to have local packages, you'll need a package.json file.

But to create the package.json file, you will have to initialize npm by running npm init.

You can then use npm install <module_name> --save[-dev].

achedeuzot
  • 4,164
  • 4
  • 41
  • 56
25

This is how you can install a module to your current directory:

npm i --prefix ./ my-app

As others have said, the proper way is to configure your project via package.json

johnpolacek
  • 2,599
  • 2
  • 23
  • 16
5

I think the real question, what I and the OP would want, is to install my-app like you would install an application. i.e. Install a top level application, that I am going to "use" as an application and not "require" as a module.

The fact that npm installs one level down from my application directory, is a purely aesthetic objection by new npm users.

When I started using npm (not so long ago), I solved it by having a git project as an installer, clone the git, run the install script, but now I am used to it and it does not bother me to have the app in the "wrong" folder any more.

Just setup some .sh, .bat or short cuts in the right place and your users won't notice.

Fudge
  • 49
  • 4
Lafras Henning
  • 133
  • 2
  • 6
4

You ought to have a package.json in your current directory.

Then write npm install <module_name> --save-dev or npm install <module_name> --save and it will install to the node_modules directory

Samyak Bhuta
  • 1,256
  • 7
  • 20
2

Using Windows 7. I had installed nodejs in a different folder (c:\develop\nodejs) to the one that was suggested while installing (C:\Program Files\nodejs) I am totally new to nodejs or npm. i may not be talking the nodejs or npm language. After installing nodejs. I had to do some commands like this in the main nodejs folder

npm init

then I was going to create a local directory (say c:\develop\nodejs\ upload2s3), and create a nodejs package to use as AWS lambda. For this, i went into the local directory (upload2s3), and tried

npm install aws-sdk

though the command behaved like it installed things, this did not do anything in my upload2s3 folder as it was empty. then i just tried this in my local folder.

npm init 

this created some package.json, and may be few other files then i tried

npm install aws-sdk

then it created a node-modules folders inside my local folder (upload2s3) and installed the aws-sdk package. It also updated the package.json file

{
  "name": "uploadtos32",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "aws-sdk": "^2.448.0"
  }
}
gsakthivel
  • 365
  • 3
  • 17
0
pack=$(npm pack <package>) ; tar xzvf ${pack} --strip-components=1 ; rm ${pack} ; npm i
dee
  • 526
  • 4
  • 8
  • 4
    While your code may well solve the problem, please try and provide an explanation as to how/why the code in your answer will help the OP. Code-only answers do not always do a great job of explaining how to solve a problem, and certainly don't pass on knowledge to others very well. – Geoff James Apr 16 '20 at 14:58
0

I also could not find any appropriate solution to simply install a standalone app from an npm registry.

You can download a tarball of the package by running npm pack <package>, extract the tarball by running tar xvf <tarball_name>.tgz and then install the dependencies by running npm install --production inside the extracted directory.

To streamline the whole process I created a bash script which could be executed directly from gist by running:

bash <(curl -sL https://gist.githubusercontent.com/mmende/2c10dd6161bc75120dbf153098caa48d/raw/installNodeApp.sh) some-package ./package-installation
mmende
  • 181
  • 1
  • 13