小编Sha*_*gmi的帖子

在另一个文件上导入时,module.exports未定义

这是我的简单Express应用程序的文件结构.

server/
  |- models
  |--- users.js
  |- index.js
Run Code Online (Sandbox Code Playgroud)

index.js 是切入点

const app = require('express')();
const Server = require('http').createServer(app)
const io = require('socket.io')(Server);
......
.....
.....
.....
....
module.exports = {
    Server,
    io
}
Run Code Online (Sandbox Code Playgroud)

在我的内部我models/users.js需要io上面导出的变量,只是在添加新用户时向所有连接的客户端发出一些事件.为此我导入了

const { io } = require('../index');
Run Code Online (Sandbox Code Playgroud)

ioundefined.我尝试过其他类似的方法

const io = require('../index').io // 
Run Code Online (Sandbox Code Playgroud)

const io = require('../').io /index is useless on require
Run Code Online (Sandbox Code Playgroud)

但都没有用.我是傻还是我在这里想念一些东西.任何帮助将受到高度赞赏.

谢谢

commonjs node.js requirejs

8
推荐指数
2
解决办法
3910
查看次数

标签 统计

commonjs ×1

node.js ×1

requirejs ×1