确保你的代码正确地使用了module.exports和require()。在本地模块中,要使用module.exports暴露一个接口,然后使用require()加载它。以下是一个示例:
创建一个名为myModule.js的本地模块并将其放置在你的项目根目录下。
在myModule.js文件中添加以下代码:
function hello() {
console.log('Hello world!');
}
module.exports = hello;
const hello = require('./myModule');
hello(); // 输出:Hello world!
确保在require()函数中传递正确的相对路径。如果你的模块不在当前目录中,将路径指向正确的位置。
如果你使用了相对路径但仍然无法正常工作,试试使用绝对路径,像这样:
const path = require('path');
const hello = require(path.resolve(__dirname, './myModule'));
hello(); // 输出:Hello world!