简体   繁体   English

Node.js中的自定义模块返回“找不到模块”错误

[英]Custom module in nodejs returns “cannot find module” error

I've been writing a little prototype app in the cloud9 ide using express and nodejs. 我一直在使用express和nodejs在cloud9 ide中编写一个小样机应用程序。

I was going to re-organize some code into a few custom modules but when I attempt to require them I get an error "cannot find module './lib' 我打算将一些代码重新组织到几个自定义模块中,但是当我尝试要求它们时,出现错误“找不到模块'./lib'

The lib folder is in the root of my site directory. lib文件夹位于我的网站目录的根目录中。 I have 1 file in there "test.js" that has a simple demo to try and get this working. 我在其中的“ test.js”中有1个文件,该文件具有一个简单的演示,可以尝试使其正常工作。

exports.test = function(){
  return 'hello world';  
};

It doesn't matter if I put require('./lib') or require('./lib/test') in my app.js, it always throws the same error. 我在我的app.js中放置require('./ lib')或require('./ lib / test')都没关系,它总是抛出相同的错误。

Any thoughts? 有什么想法吗?

Two potential errors: 两个潜在的错误:

1- require() looks for a file called index.js. 1- require()查找名为index.js的文件。 So try renaming test.js to index.js might work. 因此,尝试将test.js重命名为index.js可能有效。

2- Your path is wrong. 2-您的路径错误。 Is the lib folder in the same folder as the file in which you call the require()? lib文件夹与调用require()的文件位于同一文件夹中吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM