简体   繁体   English

npm 不断在全球安装

[英]npm keeps installing globally

I'm using latest npm, on a mac.我在 Mac 上使用最新的 npm。 I've got a strange problem: if I want to install some package locally on a folder, I'm forced to npm init it.我有一个奇怪的问题:如果我想在一个文件夹中本地安装一些包,我不得不npm init它。 In other words, if I simply npm install [package name] , it will install globally (in my /User/npm-modules folder), and thus the onnly way to install locally is to init the folder.换句话说,如果我只是npm install [package name] ,它将全局安装(在我的 /User/npm-modules 文件夹中),因此在本地安装的唯一方法是初始化文件夹。 So basically, the opposite it should normally do.所以基本上,它通常应该做的相反。 Why?为什么? Any idea?任何的想法?

npm init creates package.json file in your current project directory. npm init在当前项目目录中创建package.json文件。 npm packages will install locally when you have package.json set up by using the command npm i PACKAGE-NAME . 当你使用命令npm i PACKAGE-NAME设置package.json时,npm包将在本地安装。

Yes, if you want to install package localy, you need to create package.json file. 是的,如果要安装软件包localy,则需要创建package.json文件。 This is what npm init will do and then you can install packages 这是npm init将要执行的操作,然后您可以安装包

I haven't worked with node in a while but back in my days, npm used to look for node_modues folder starting from current directory and keeps moving to the parent folder. 我在一段时间内没有使用过节点,但在我的日子里,npm曾经从当前目录开始查找node_modues文件夹并继续移动到父文件夹。 If no node_modules folder is found then it will go to the global folder. 如果找不到node_modules文件夹,则它将转到全局文件夹。

So in your case create an empty node_modules folder in your current directory and try npm install [package] 因此,在您的情况下,在当前目录中创建一个空的node_modules文件夹并尝试npm install [package]

This should avoid the FORCED npm init you mentioned. 这应该避免你提到的FORCED npm init However, a cleaner solution is always to use npm init . 但是,更npm init解决方案始终是使用npm init

The npm idea was to separate different apps and their dependencies in a single project. npm的想法是在一个项目中分离不同的应用程序及其依赖项。 Looking for node-modules in the same folder helped to achieve this. 在同一文件夹中查找节点模块有助于实现此目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM