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

查看:33
本文介绍了Nodejs 在 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) odejs ode_modules" 目录中,但以下代码将失败并显示 "Cannot find module 'jade'" 错误,

Jade is installed in directory "C:Program Files (x86) odejs ode_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 pm ode_modules (Windows XP)、%AppData% pm ode_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 pm ode_modules (Windows XP), %AppData% pm ode_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%
pm
ode_modules
rem for current session
set NODE_PATH=%AppData%
pm
ode_modules

值得一提的是,NODE_PATH 仅在 Node 应用中导入模块时使用.当你想在你的 CLI 中使用全局安装的模块的二进制文件时,你还需要将它添加到你的 PATH,但没有 node_modules 部分(例如 %AppData% pm 在 Windows 7/8/10 中).

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% pm 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 不是安装全局库的方法,它只是将它们放在系统路径上的一种方法,这样您就可以从命令行调用它们而无需写入它们的完整路径.它很有用,例如,node 应用程序正在转换本地文件,例如 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?

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

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