Error: bind EADDRINUSE when hot module replacement enabled in neutrino node
Error: bind EADDRINUSE when hot module replacement enabled in neutrino node
我正在使用 neutrino node.js preset 并启用了热模块更换,这是我在 index.js
中的内容:
import fastify from 'fastify'
import router from './router'
const ft = fastify()
ft.register(router)
// enable hot module replacement
if (module.hot) {
module.hot.accept()
}
// listen
ft.listen(3000, 'localhost', (err) => {
if (err) throw err
console.log(`server listening on ${ft.server.address().port}`)
})
当我编辑一个文件时,我会得到这个:
我想知道是否可以像这样使用热模块更换?有人有经验吗?
谢谢。
在 Node.js 中使用 HMR 时,它比 Web 项目要复杂一些,因为重新加载顶级文件意味着需要重新启动进程。
如果您注意到 Neutrino docs on HMR in Node.js,您会发现 HMR 不是在顶级 index.js
文件上执行的,而是在 index.js
导入的项目上执行的.
因此,您需要接受特定的东西 index.js
导入,并从内部使用它们,而不是尝试重新加载 index.js
,如果不重新启动进程就无法工作。
if (module.hot) {
module.hot.accept('./router');
// Optionally use a function to re-bind functionality
// after the accept:
// module.hot.accept('./router', () => /* ... */);
}
我正在使用 neutrino node.js preset 并启用了热模块更换,这是我在 index.js
中的内容:
import fastify from 'fastify'
import router from './router'
const ft = fastify()
ft.register(router)
// enable hot module replacement
if (module.hot) {
module.hot.accept()
}
// listen
ft.listen(3000, 'localhost', (err) => {
if (err) throw err
console.log(`server listening on ${ft.server.address().port}`)
})
当我编辑一个文件时,我会得到这个:
我想知道是否可以像这样使用热模块更换?有人有经验吗?
谢谢。
在 Node.js 中使用 HMR 时,它比 Web 项目要复杂一些,因为重新加载顶级文件意味着需要重新启动进程。
如果您注意到 Neutrino docs on HMR in Node.js,您会发现 HMR 不是在顶级 index.js
文件上执行的,而是在 index.js
导入的项目上执行的.
因此,您需要接受特定的东西 index.js
导入,并从内部使用它们,而不是尝试重新加载 index.js
,如果不重新启动进程就无法工作。
if (module.hot) {
module.hot.accept('./router');
// Optionally use a function to re-bind functionality
// after the accept:
// module.hot.accept('./router', () => /* ... */);
}