Node.js在Windows上找不到已安装的模块 [英] Nodejs cannot find installed module on Windows

查看:156
本文介绍了Node.js在Windows上找不到已安装的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在Windows上学习nodejs.通过npm.cmd在全局安装了几个模块,nodejs无法找到已安装的模块.以玉为例,

I am learning nodejs at the moment on Windows. Several modules are installed globally with npm.cmd, and nodejs failed to find the installed modules. Take jade for example,

npm install jade -g

Jade安装在目录"C:\Program Files (x86)\nodejs\node_modules"中,但是以下代码将失败,并出现"Cannot find module 'jade'"错误,

Jade is installed in directory "C:\Program Files (x86)\nodejs\node_modules", but the following code will fail with a "Cannot find module 'jade'" error,

var jade = require('jade');

但是,在本地安装Jade时,代码将成功运行(npm中没有-g选项).我不想使用本地安装的模块,这对我来说是浪费磁盘空间.如何使全局安装的模块在Windows上工作?

However, the code will run successfully when jade is locally installed (without -g option in npm). I don't want to use locally-installed modules, it's a waste of disk space for me. How can I make the globally-installed modules work on Windows?

推荐答案

添加一个名为NODE_PATH的环境变量,并将其设置为%USERPROFILE%\Application Data\npm\node_modules(Windows XP),%AppData%\npm\node_modules(Windows 7/8/10)或无论npm最终在Windows风味上安装模块的位置.要一劳永逸地完成此操作,请将其添加为系统属性"对话框的高级"选项卡中的系统"变量(运行control.exe sysdm.cpl,System,3).

Add an environment variable called NODE_PATH and set it to %USERPROFILE%\Application Data\npm\node_modules (Windows XP), %AppData%\npm\node_modules (Windows 7/8/10), or wherever npm ends up installing the modules on your Windows flavor. To be done with it once and for all, add this as a System variable in the Advanced tab of the System Properties dialog (run control.exe sysdm.cpl,System,3).

Windows 7+中的快速解决方案是仅运行:

Quick solution in Windows 7+ is to just run:

rem for future
setx NODE_PATH %AppData%\npm\node_modules
rem for current session
set NODE_PATH=%AppData%\npm\node_modules

值得一提的是,NODE_PATH仅在Node应用程序中导入模块时使用.如果要在CLI中使用全局安装的模块的二进制文件,则需要将其也添加到PATH中,但没有node_modules部分(例如Windows 7/8/10中的%AppData%\npm).

It's worth to mention that NODE_PATH is only used when importing modules in Node apps. When you want to use globally installed modules' binaries in your CLI you need to add it also to your PATH, but without node_modules part (for example %AppData%\npm in Windows 7/8/10).

旧故事

我本人对node.js相当陌生,所以我可能并不完全正确,但是根据我的经验,它是这样工作的:

I'm pretty much new to node.js myself so I can be not entirely right but from my experience it's works this way:

  1. -g 不是安装全局库的方法,它只是将它们放置在系统路径上的一种方法,因此您可以从命令行调用它们,而无需编写它们的完整路径.例如,这很有用,然后节点应用程序将转换本地文件,例如 less -如果在全局范围内安装它,则可以在任何目录中使用它.
  2. node.js本身并未查看npm全局目录,而是使用另一种算法来查找所需文件: http://nodejs.org/api/modules.html#modules_file_modules (基本上是扫描路径中的每个文件夹,从当前的 node_modules 文件夹开始并进行检查).
  1. -g is not a way to install global libraries, it's only a way to place them on system path so you can call them from command line without writing the full path to them. It is useful, for example, then node app is converting local files, like less — if you install it globally you can use it in any directory.
  2. node.js itself didn't look at the npm global dir, it is using another algorithm to find required files: http://nodejs.org/api/modules.html#modules_file_modules (basically its scanning every folder in the path, starting from the current for node_modules folder and checks it).

有关更多详细信息,请参见类似的问题:如何使用npm全局安装模块?

See similar question for more details: How do I install a module globally using npm?

这篇关于Node.js在Windows上找不到已安装的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆