npm install module in current directory

2020-02-07 16:42发布

问题:

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?

回答1:

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.



回答2:

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].



回答3:

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



回答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



回答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.



回答6:

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"
  }
}


标签: node.js npm