简体   繁体   English

为什么节点要全局安装而不是本地安装?

[英]Why is node installing globally instead of locally?

So I navigated to my project directory and run npm install scrollama intersection-observer . 因此,我导航到我的项目目录并运行npm install scrollama intersection-observer It installed the files on my HOME/node-modules instead of the project folder. 它将文件安装在我的HOME / node-modules上,而不是项目文件夹中。 During installation there were warnings, though: 但是,在安装过程中会出现警告:

npm WARN saveError ENOENT: no such file or directory, open '[my-home]/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '[my-home]/package.json'
npm WARN [my-user] No description
npm WARN [my-user] No repository field.
npm WARN [my-user] No README data
npm WARN [my-user] No license field.

So why did it installed globally (or, in my home instead of my project folder)? 那么,为什么要在全球范围内(或在我的家中而不是在项目文件夹中)安装它?

Because your folder is not a valid npm install location as it could not find the package.json . 因为您的文件夹不是有效的npm安装位置,所以找不到package.json to create that, just run: 要创建它,只需运行:

 npm init

You are not using npm init . 您没有使用npm init npm init will initialize your node project and will make a package.json file. npm init将初始化您的节点项目并创建一个package.json文件。 package.json file will store the information about the project such as project name, version, description and also the dependencies you download. package.json文件将存储有关项目的信息,例如项目名称,版本,描述以及您下载的依赖项。 In your case there is no package.json file, because of this your packages are been installed to the home directory instead of current directory. 在您的情况下,没有package.json文件,因此,您的软件包已安装到主目录而不是当前目录。

Also you can also use parameters like -g to install package globally (to the home directory) without any parameter, node will install the package in the current directory by default. 另外,您也可以使用-g之类的参数在没有任何参数的情况下全局安装软件包(到主目录),默认情况下,节点会将软件包安装在当前目录中。

Installing locally: you can use the package inside your project. 在本地安装:您可以在项目中使用该软件包。 Installing globally: you can use the package everywhere. 全局安装:您可以在任何地方使用该软件包。 Commands like nodemon are installed globally because you want to use these in every project. 像nodemon这样的命令是全局安装的,因为您想在每个项目中使用它们。

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

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