Error: Cannot find module 'formidable'

最近在學習Node.js,看《Node入門 —— 一本全面的Node.js教程》。其中一個例子用到了“formidable”,使用npm(npm -g install formidable)安裝後,在js中使用require引入(var formidable = require("formidable");),執行時報錯,詳細信息如下:

Error: Cannot find module 'formidable'
    at Function.Module._resolveFilename (module.js:555:15)
    at Function.Module._load (module.js:482:25)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (e:\PrivatedData\programs\NodeJS\prog1\requestHandlers.js:3:18)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)

在網上查資料,說要把項目放到Node的安裝目錄下,我覺得不好,應該有其他方法。

試着在require時,寫入模塊的全路徑,即

var formidable = require("x:\\nodejs\\node_modules\\formidable");

運行通過。

原因個人覺得對於Node的核心模塊,可以直接寫模塊名,如  require("fs"),而非核心模塊(所謂“文件模塊”),需要讓node找到模塊,所以要寫全路徑。(菜鳥見識,大神勿噴。)

補充說明:

上面是使用“全局安裝”來安裝formidable。根據runoob的Node.js教程中的“npm使用介紹”(菜鳥教程 http://www.runoob.com/nodejs/nodejs-npm.html)一文,可以在項目目錄下使用npm“本地安裝”formidable時,require不需要全路徑。按照教程試了一下,沒有問題。

-----------------------------------------------------------------

另,在同一例子中,還遇到了“”問題。此問題在網上有解決,請參閱“解決Node.js調用fs.renameSync報錯的問題(Error: EXDEV, cross-device link not permitted)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章