Where does npm install packages?

2019-01-01 04:23发布

Can someone tell me where can I find the Node.js modules, which I installed using npm?

17条回答
浪荡孟婆
2楼-- · 2019-01-01 04:50

If you're trying to access your global dir from code, you can backtrack from process.execPath. For example, to find wsproxy, which is in {NODE_GLOBAL_DIR}/bin/wsproxy, you can just:

path.join(path.dirname(process.execPath), 'wsproxy')
查看更多
旧时光的记忆
3楼-- · 2019-01-01 04:52

Global libraries

You can run npm list -g to see where global libraries are installed.

On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node.

Windows XP - %USERPROFILE%\AppData\npm\node_modules
Windows 7, 8 and 10 - %USERPROFILE%\AppData\Roaming\npm\node_modules

Non-global libraries

Non-global libraries are installed the node_modules sub folder in the folder you are currently in.

You can run npm list to see the installed non-global libraries for your current location.

查看更多
不再属于我。
4楼-- · 2019-01-01 04:53

If you are looking for the executable that npm installed, maybe because you would like to put it in your PATH, you can simply do

npm bin

or

npm bin -g
查看更多
时光乱了年华
5楼-- · 2019-01-01 04:53

If module was installed with global (-g) flag, you can get parent location by running following command

npm get prefix

or

npm ls -g --depth=0

which will print location along with list of installed modules

Cheers :-)

查看更多
素衣白纱
6楼-- · 2019-01-01 04:53

From the docs:

Packages are dropped into the node_modules folder under the prefix. When installing locally, this means that you can require("packagename") to load its main module, or require("packagename/lib/path/to/sub/module") to load other modules.

Global installs on Unix systems go to {prefix}/lib/node_modules. Global installs on Windows go to {prefix}/node_modules (that is, no lib folder.)

Scoped packages are installed the same way, except they are grouped together in a sub-folder of the relevant node_modules folder with the name of that scope prefix by the @ symbol, e.g. npm install @myorg/package would place the package in {prefix}/node_modules/@myorg/package. See scope for more details.

If you wish to require() a package, then install it locally.

You can get your {prefix} with npm config get prefix. (Useful when you installed node with nvm).

Read about locally.
Read about globally.

查看更多
君临天下
7楼-- · 2019-01-01 04:54

Not direct answer but may help ....

The npm also has a cache folder, which can be found by running npm config get cache (%AppData%/npm-cache on Windows).

The npm modules are first downloaded here and then copied to npm global folder (%AppData%/npm/Roaming on Windows) or project specific folder (your-project/node_modules).

So if you want to track npm packages, and some how, the list of all downloaded npm packages (if the npm cache is not cleaned) have a look at this folder. The folder structure is as {cache}/{name}/{version}

This may help also https://docs.npmjs.com/cli/cache

查看更多
登录 后发表回答