【问题标题】:Error: Cannot find module "."错误:找不到模块“。”
【发布时间】:2017-08-02 10:36:14
【问题描述】:

我正在使用 Angular Universal、Express 和 Webpack。我使用这个 git repo 启动了应用程序:

https://github.com/FrozenPandaz/ng-universal-demo

我似乎无法在 main.server.ts 中使用 socket.io。我可以导入它,但是在尝试设置它时出现错误:

...
import * as express from 'express';
import * as socket from 'socket.io';

enableProdMode();
const app = express();
const server = require('http').Server(app);
const io = socket(server);
// .       ^^ causes error 

app.engine('html', ngExpressEngine({
  bootstrap: ServerAppModule
}));

app.set('view engine', 'html');
app.set('views', 'src');

app.use('/', express.static('dist', {index: false}));

server.listen(1337);

app.get('test', (req, res) => {
  res.render('../dist/index', {
    req: req,
    res: res
  });
});

io.on('connection', (socket) => {
  socket.on('register', (data) => {
    socket.join(data);
    user = data;
  });
});

由于socket(server) 调用而发生错误,就好像我将其注释掉一样,错误不会发生:

/Users/michaelwilson/Code/app/dist/server.js:101828
    return /*require.resolve*/(!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()));
                                                                                                                                            ^

Error: Cannot find module "."
    at webpackMissingModule (/Users/michaelwilson/Code/app/dist/server.js:101828:76)
    at resolvePath (/Users/michaelwilson/Code/app/dist/server.js:101828:154)
    at Server.serveClient (/Users/michaelwilson/Code/app/dist/server.js:101831:25)
    at new Server (/Users/michaelwilson/Code/app/dist/server.js:101770:8)
    at Server (/Users/michaelwilson/Code/app/dist/server.js:101762:41)
    at Object.<anonymous> (/Users/michaelwilson/Code/app/dist/server.js:113993:10)
    at __webpack_require__ (/Users/michaelwilson/Code/app/dist/server.js:26:30)
    at /Users/michaelwilson/Code/app/dist/server.js:94:18
    at Object.<anonymous> (/Users/michaelwilson/Code/app/dist/server.js:97:10)
    at Module._compile (module.js:570:32)
[nodemon] app crashed - waiting for file changes before starting...

我不确定还能尝试什么,这是 Webpack + Socket.io 的问题吗?我找不到其他有同样问题的人...

编辑:我已经找到了在 socket.io 库中发生的代码行,它试图进行动态解析:

return /*require.resolve*/(!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()));

【问题讨论】:

  • 检查:stackoverflow.com/questions/34823655/… 另外,请确保您已安装正确引用的那些包(它们是否在 package.json 和 node_modules 文件夹中?)
  • @EdmundoRodrigues 该错误与我引用的任何模块无关,如果是这种情况,它将在 socket.io 本身内。但事实上它是“。”哪个找不到,好像不是这样……

标签: angular webpack socket.io angular-universal


【解决方案1】:

我设法通过将serveClient 属性设置为false 来消除此错误,如下所示:

const io = require('socket.io')(server, {
  serveClient: false,
  wsEngine: 'ws' // uws is not supported since it is a native module
});

https://github.com/socketio/socket.io/tree/master/examples/webpack-build-server https://socket.io/docs/server-api/#server-serveclient-value

【讨论】:

    猜你喜欢
    • 2018-12-30
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 2023-04-03
    • 2017-01-21
    • 2018-06-24
    相关资源
    最近更新 更多