【问题标题】:node.js express module not loading properlynode.js express 模块未正确加载
【发布时间】:2011-08-30 04:53:17
【问题描述】:
Geos-Mac:hone georgiana$ npm list
/Users/georgiana/local/hone
├─┬ express@2.3.6 
│ ├── connect@1.4.1 
│ ├── mime@1.2.2 
│ └── qs@0.1.0 
├── gently@0.9.0 
└── mysql@0.9.1

但是,一个包含

的简单脚本
var express = require( 'express' ).createServer();

给了

Error: Cannot find module 'express'
at Function._resolveFilename (module.js:322:11)
.........

尝试使用 express 时。

请注意

Geos-Mac:hone georgiana$ npm express -v
1.0.6

Geos-Mac:hone georgiana$ node -v
v0.5.0-pre

有什么想法吗?

【问题讨论】:

  • 如果你在 node shell 中输入:require.paths,你会看到什么?
  • npm 在本地安装模块。所以你的代码需要靠近你的/.node_modules/ 文件夹。
  • require_paths 输出> require.paths [ '/Users/georgiana/.node_modules', '/Users/georgiana/.node_libraries', '/Users/georgiana/local/hone/lib/node' ]> Geos-Mac:node_modules georgiana$ pwd/Users/georgiana/local/hone/node_modulesGeos-Mac:node_modules georgiana$ ls -lsatotal 00 drwxr-xr-x 5 georgiana staff 170 May 21 13:55 .0 drwxr-xr-x 5 georgiana staff 170 May 21 13:55 .0 drwxr-xr-x 7 georgiana staff 238 May 21 13:54 ..0 drwxr-xr-x 7 georgiana staff 238 May 21 13:54 ..0 drwxr-xr-x 2 georgiana staff 68 May 21 13:55 .bin0 drwxr-xr-x 9 georgiana staff 306 May 21 01:01 gentlypan@98765s>338s
  • 通过在应用程序文件夹中安装 express 模块解决;受到手册nodejs.org/docs/v0.4.1/api/… 的启发,所以在/Users/georgiana/Sites/nodejs-play 内部我执行了npm install express 命令,该命令将模块安装在/Users/georgiana/Sites/nodejs-play/node_modules/express

标签: node.js load express


【解决方案1】:

通过在应用程序文件夹中安装 express 模块解决;灵感来自手册http://nodejs.org/docs/v0.4.1/api/modules.html#loading_from_node_modules_Folders

所以,在/Users/georgiana/Sites/nodejs-play 内部我执行了npm install express 命令,该命令将模块安装在/Users/georgiana/Sites/nodejs-play/node_modules/express

【讨论】:

    猜你喜欢
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多