简体   繁体   English

全局或本地节点安装包

[英]Node install packages globaly or locally

I installed create-react-app package locally.我在本地安装了 create-react-app 包。 But it did not work.但它没有用。 When I installed it globally using -g , it worked.当我使用-g全局安装它时,它起作用了。

I have came across several other packages which some of them works when installed globally but not locally and vice versa.我遇到了其他几个包,其中一些包在全局安装时可以工作,但在本地无法安装,反之亦然。

How someone know a particular package to install globally or locally?有人如何知道要在全球或本地安装的特定软件包?

Use npx create-react-app , this will download the React Cli and after creating the project it will delete the Cli from your local system.使用npx create-react-app ,这将下载React Cli并在创建项目后从本地系统中删除Cli

npm will fist scan the local node_module folder in the project if it find the package then it will use it, otherwise it will go up to the tree and search in the the global package. npm 将首先扫描项目中的本地 node_module 文件夹,如果找到包则使用它,否则它将上树并在全局包中搜索。

All project dependency can be used globally or locally in project.所有项目依赖都可以在项目中全局或本地使用。

But in this case create-react-app is used as a shell tool not as a project dependency so it has to be in PATH environment variable.但在这种情况下, create-react-app用作 shell 工具而不是项目依赖项,因此它必须位于 PATH 环境变量中。

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.如果您要安装要在程序中使用的东西,请使用 require('whatever'),然后在本地安装,位于项目的根目录下。

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.如果您要在 shell、命令行或其他地方安装要在 shell 中使用的东西,请全局安装它,以便它的二进制文件最终出现在您的 PATH 环境变量中。

Try this, hope this will solve the issue.试试这个,希望这能解决问题。

I dont think this should happen.我不认为这应该发生。 You can follow this document to know about global vs local setup :您可以按照此文档了解全局与本地设置

https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/ https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/

  • npm is a tool mainly used to install packages. npm 是一个主要用于安装包的工具。
  • npx is a tool to execute packages. npx 是一个执行包的工具。

What does it means?这是什么意思? That if you want to execute a package without installing it on your computer and then launch it you can use npx directly.如果你想执行一个包而不在你的计算机上安装它然后启动它,你可以直接使用 npx。 for eg;例如;

  • create-react-app is an utility to bootstrap a react project. create-react-app 是一个用于引导 React 项目的实用程序。
  • if you use it with npx ( npx create-react-app my-app ) you will have your my-app project in place without the need to install create-react-app itself.如果您将它与 npx ( npx create-react-app my-app ) 一起使用,您将拥有 my-app 项目,而无需安装 create-react-app 本身。
  • npm install create-react-app and then create-react-app my-app if you use npm npm install create-react-app 然后 create-react-app my-app 如果你使用 npm

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

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