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?
npm install
installs packages either locally or globally:
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.-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:
- 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.- 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.
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]
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With