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

查看:75
本文介绍了如何在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 <global-package>创建指向已全局安装的软件包的本地链接. (注意:操作系统必须支持符号链接.)

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链接是一个开发工具.在您的本地开发环境中管理软件包是棒极了.但是,使用npm链接进行部署本质上是在问问题,因为它使得在没有意识到的情况下更新事物变得非常容易.

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.

有关其他信息,请参见

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

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

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