如何在 Node 中导入全局模块?我收到“错误:找不到模块 <模块>"? [英] How do I import global modules in Node? I get &quot;Error: Cannot find module &lt;module&gt;&quot;?

查看:37
本文介绍了如何在 Node 中导入全局模块?我收到“错误:找不到模块 <模块>"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Mac OSX Lion 上设置 Node.一切似乎都正常,但我似乎无法从我的全局模块文件夹中导入任何模块.我收到错误,

I am trying to setup Node on Mac OSX Lion. It all seems to work ok, but I can't seem to import anything modules from my global modules folder. I get the error,

Error: Cannot find module <module>

如果我运行这个:node -e require.paths,我得到的响应是:

If I run this: node -e require.paths, the response I get is:

[ '/usr/local/lib/node_modules',
  '/Users/Me/.node_modules',
  '/Users/Me/.node_libraries',
  '/usr/local/Cellar/node/0.4.12/lib/node' ]

这是正确的,我的模块确实安装在/usr/local/lib/node_modules 中.然而,当我尝试运行脚本时,我得到了这个:

Which is correct, my modules are indeed installed in /usr/local/lib/node_modules. When I try and run a script, however, I am getting this:

Error: Cannot find module 'socket.io'
    at Function._resolveFilename (module.js:326:11)
    at Function._load (module.js:271:25)
    at require (module.js:355:19)
    at Object.<anonymous> (/Users/Me/node/server.js:2:10)
    at Module._compile (module.js:411:26)
    at Object..js (module.js:417:10)
    at Module.load (module.js:343:31)
    at Function._load (module.js:302:12)
    at Array.<anonymous> (module.js:430:10)
    at EventEmitter._tickCallback (node.js:126:26)

我的 .bash_profile 如下所示:

My .bash_profile looks like this:

export PATH=/usr/local/mysql/bin:$PATH
export NODE_PATH=/usr/local/lib/node_modules
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/"

非常感谢您的帮助,我不知道为什么我无法导入任何库.

Would really appreciate some help, I have no idea why I can't import any libraries.

推荐答案

如果你使用的是 npm >=1.0,你可以使用 npm link 创建本地链接到一个已经全局安装的包.(警告:操作系统必须支持符号链接.)

If you're using npm >=1.0, you can use npm link <global-package> to create a local link to a package already installed globally. (Caveat: The OS must support symlinks.)

然而,这并非没有问题.

However, this doesn't come without its problems.

npm link 是一个开发工具.在您的本地开发箱上管理包很棒.但是使用 npm link 进行部署基本上是在寻找问题,因为它可以在不知不觉中更新内容变得非常容易.

npm link is a development tool. It's awesome for managing packages on your local development box. But deploying with npm link is basically asking for problems, since it makes it super easy to update things without realizing it.

或者,您可以在本地和全局安装软件包.

As an alternative, you can install the packages locally as well as globally.

有关其他信息,请参阅

这篇关于如何在 Node 中导入全局模块?我收到“错误:找不到模块 <模块>"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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