简体   繁体   English

运行 npm 全局安装包

[英]Running npm globally installed packages

Can someone please explain how do node's globally installed behave.有人可以解释一下节点的全局安装行为如何吗? It is really confusing me.这真的让我很困惑。 If I install a package (with executables) such as http-server globally I can run it with:如果我在全球范围内安装一个包(带有可执行文件),例如http-server我可以运行它:

http-server

But if I do但如果我这样做

node http-server

I get我明白了

module.js:339
    throw err;
    ^

Error: Cannot find module '/path/to/current/dir/http-server'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:136:18)
    at node.js:972:3

I suspect my tern package in emacs is trying to run it with node hence breaking.我怀疑我在emacs中的tern包试图用node运行它,因此中断了。 Why is this happening?为什么会这样? Why can't node find the path to it's own modules?为什么节点找不到它自己的模块的路径?

There are two ways installing packages: globally and locally .安装包有两种方式: globallylocally
Locally installed package files end up in your local node_modules (in your project folder where you called npm install some-package ).本地安装的包文件最终位于本地node_modules中(在您调用npm install some-package的项目文件夹中)。
Globally installed package files end up in your system so they are available in command line, if globally installed packages provides executable then you can invoke it in command line directly some-package (without node ), if it does not provide executable then you can use it in repl mode ( node ) like var package = require('some-package') and it is also available locally (inside your project folder even if you don't have it installed locally).全局安装的包文件最终会出现在您的系统中,因此它们可以在命令行中使用,如果全局安装的包提供可执行文件,那么您可以在命令行中直接调用它some-package (没有node ),如果它不提供可执行文件,那么您可以使用它在 repl 模式 ( node ) 中,如var package = require('some-package')并且它也可以在本地使用(在你的项目文件夹中,即使你没有在本地安装它)。

This started as a comment but got now a little longer.这开始是评论,但现在有点长了。

The problem is not exactly node not finding global packages, node only searches for packages in the current location (like under under node_modules ), and that is by design.问题不完全是node没有找到全局包, node只在当前位置搜索包(比如在node_modules下),这是设计使然。 Globally installed packages can be run from the command like because of the way npm installs them, and this is what makes global packages special in some way.由于npm安装它们的方式,全局安装的包可以从命令 like 运行,这就是使全局包在某种程度上特别的原因。

On Unix based systems, npm creates soft links to the main executables of globally installed packages, like http-server in a folder in the executable path.在基于 Unix 的系统上,npm 创建指向全局安装包的主要可执行文件的软链接,例如可执行路径中文件夹中的http-server On my machine, this is /usr/local/bin/ .在我的机器上,这是/usr/local/bin/ This is why those commands can be invoked from the command line without specifying a full path.这就是为什么可以在不指定完整路径的情况下从命令行调用这些命令的原因。

On Windows, npm creates an executable batch file named for instance http-server.cmd under %APPDATA% (typically something like C:\Users\YourUserName\AppData\Roaming ).在 Windows 上,npm 在%APPDATA%下创建一个名为http-server.cmd的可执行批处理文件(通常类似于C:\Users\YourUserName\AppData\Roaming )。 The batch file contains instructions to run the target executable from the location where it's actually installed.批处理文件包含从实际安装位置运行目标可执行文件的说明。

rahul@Rahul-Machine:~$ node blalal 
module.js:338
throw err;
^

Error: Cannot find module '/home/rahul/blalal'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3

ooh same error哦同样的错误

this is because i first command you are actually trying to access a global variable but in second you are some where in your file hierarchy and from there you are saying that you want to access that package so you are wrong if you want to execute that global package try这是因为我首先命令你实际上是在尝试访问一个全局变量,但在第二个命令中,你在文件层次结构中的某个位置,从那里你说你想访问那个包,所以如果你想执行那个全局变量,那你就错了包试

whereis http-server

then go to that directory and find the file package.json and then open it and find the "main" property and there you get a file name then type然后转到该目录并找到文件 package.json 然后打开它并找到“main”属性,在那里你会得到一个文件名然后键入

  node index.js

your file will be executed你的文件将被执行

This answer will help you run the npm node module on the command line if you haven't installed it globally.如果您尚未全局安装 npm 节点模块,此答案将帮助您在命令行上运行它。 Either run it globally as they way you are doing.按照您的方式在全球范围内运行它。 Other option is to give full path of local package file.其他选项是提供本地包文件的完整路径。 For example I want to run a package live-server which is installed as package locally in my current directory.例如,我想运行一个包 live-server,它作为包本地安装在我的当前目录中。

node ./node_modules/live-server/live-server --port=5000 

Here(on my Mac) the live-server.js file is inside the live-server directory, Its optional to add.js and execute the command as below.这里(在我的 Mac 上)live-server.js 文件在 live-server 目录中,它是可选的 add.js 并执行如下命令。 Also port is optional argument for live-server端口也是实时服务器的可选参数

node ./node_modules/live-server/live-server.js --port=5000 

When you install something globally you store a variable with a stored path linking to it as well as the execution program.当您全局安装某些东西时,您会存储一个变量,该变量具有链接到它的存储路径以及执行程序。 While your operating system will know how to access it.而您的操作系统将知道如何访问它。 Node will not.节点不会。 If you want to "node something.js" you much either be in the directory it is or adjust your path so that node knows how to get to the file.如果你想“节点 something.js”,你要么在它所在的目录中,要么调整你的路径,以便节点知道如何访问该文件。 for instance node "c:/jsapps/main/app.js" or if you were in lets say the folder 'jsapps' you would type node "main/app.js" to execute the same file.例如节点“c:/jsapps/main/app.js” ,或者如果你在文件夹“jsapps”中,你将键入节点“main/app.js”来执行相同的文件。

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

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