使用T3 Stack启动Next.js服务触发node::loader::ImportModuleDynamically错误求助
问题:Next.js + T3 Stack启动触发Node断言错误
我正在使用T3 Stack,所有基于Next.js启动服务器的项目(包括当前项目)都会抛出以下错误:
node[20481]: ../src/module_wrap.cc:599:MaybeLocal<v8::Promise> node::loader::ImportModuleDynamically(Local<v8::Context>, Local<v8::Data>, Local<v8::Value>, Local<v8::String>, Local<v8::FixedArray>): Assertion `(it) != (env->id_to_function_map.end())' failed. 1: 0x102f7e100 node::Abort() [/opt/homebrew/Cellar/node/19.8.0/bin/node] 2: 0x102f7e0e4 node::Abort() [/opt/homebrew/Cellar/node/19.8.0/bin/node] 3: 0x102f4183c node::loader::ImportModuleDynamically(v8::Local<v8::Context>, v8::Local<v8::Data>, v8::Local<v8::Value>, v8::Local<v8::String>, v8::Local<v8::FixedArray>) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 4: 0x103215e54 v8::internal::Isolate::RunHostImportModuleDynamicallyCallback(v8::internal::MaybeHandle<v8::internal::Script>, v8::internal::Handle<v8::internal::Object>, v8::internal::MaybeHandle<v8::internal::Object>) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 5: 0x1035b7724 v8::internal::Runtime_DynamicImportCall(int, unsigned long*, v8::internal::Isolate*) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 6: 0x102dd3524 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvInRegister_NoBuiltinExit [/opt/homebrew/Cellar/node/19.8.0/bin/node] 7: 0x102e7849c Builtins_CallRuntimeHandler [/opt/homebrew/Cellar/node/19.8.0/bin/node] 8: 0x102d50064 Builtins_InterpreterEntryTrampoline [/opt/homebrew/Cellar/node/19.8.0/bin/node] 9: 0x102d868b4 Builtins_AsyncFunctionAwaitResolveClosure [/opt/homebrew/Cellar/node/19.8.0/bin/node] 10: 0x102e26e38 Builtins_PromiseFulfillReactionJob [/opt/homebrew/Cellar/node/19.8.0/bin/node] 11: 0x102d76834 Builtins_RunMicrotasks [/opt/homebrew/Cellar/node/19.8.0/bin/node] 12: 0x102d4e3c4 Builtins_JSRunMicrotasksEntry [/opt/homebrew/Cellar/node/19.8.0/bin/node] 13: 0x1031fad74 v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 14: 0x1031fb4bc v8::internal::(anonymous namespace)::InvokeWithTryCatch(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 15: 0x10321df90 v8::internal::MicrotaskQueue::RunMicrotasks(v8::internal::Isolate*) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 16: 0x10321ddc0 v8::internal::MicrotaskQueue::PerformCheckpointInternal(v8::Isolate*) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 17: 0x102eb0ae8 node::InternalCallbackScope::Close() [/opt/homebrew/Cellar/node/19.8.0/bin/node] 18: 0x102eb0ff4 node::InternalMakeCallback(node::Environment*, v8::Local<v8::Object>, v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*, node::async_context) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 19: 0x102ecc7a8 node::AsyncWrap::MakeCallback(v8::Local<v8::Function>, int, v8::Local<v8::Value>*) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 20: 0x102f852ec node::fs::FSReqCallback::Resolve(v8::Local<v8::Value>) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 21: 0x102f86cec node::fs::AfterStat(uv_fs_s*) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 22: 0x102f78438 node::MakeLibuvRequestCallback<uv_fs_s, void (*)(uv_fs_s*)>::Wrapper(uv_fs_s*) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 23: 0x105a26ff0 uv__work_done [/opt/homebrew/Cellar/libuv/1.44.2/lib/libuv.1.dylib] 24: 0x105a2a3c4 uv__async_io [/opt/homebrew/Cellar/libuv/1.44.2/lib/libuv.1.dylib] 25: 0x105a3a1e0 uv__io_poll [/opt/homebrew/Cellar/libuv/1.44.2/lib/libuv.1.dylib] 26: 0x105a2a7bc uv_run [/opt/homebrew/Cellar/libuv/1.44.2/lib/libuv.1.dylib] 27: 0x102eb18a8 node::SpinEventLoopInternal(node::Environment*) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 28: 0x102fc01ec node::NodeMainInstance::Run(node::ExitCode*, node::Environment*) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 29: 0x102fbff6c node::NodeMainInstance::Run() [/opt/homebrew/Cellar/node/19.8.0/bin/node] 30: 0x102f46830 node::LoadSnapshotDataAndRun(node::SnapshotData const**, node::InitializationResultImpl const*) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 31: 0x102f469b4 node::Start(int, char**) [/opt/homebrew/Cellar/node/19.8.0/bin/node] 32: 0x105c8108c [1] 20481 abort next
已尝试以下操作但无效:
- 重新安装依赖包
- 重建node_modules文件夹
- 全局安装Next.js
解决方案
1. 降级Node.js到LTS版本
当前使用的Node.js 19.8.0属于非LTS版本,存在动态导入相关的已知bug。建议切换到18.x或20.x LTS版本:
- 用nvm切换:
nvm install 18 nvm use 18 - 用Homebrew切换:
brew install node@18 brew link --overwrite node@18
2. 确保Next.js版本兼容性
锁定Next.js到与LTS Node兼容的稳定版本,修改项目package.json:
"next": "^13.4.0"
然后执行npm install重新安装依赖。
3. 临时禁用SWC编译器
在next.config.js中添加配置禁用SWC,改用Babel编译:
module.exports = { swcMinify: false, }
4. 清理Node缓存并重建依赖
npm cache clean --force rm -rf node_modules package-lock.json npm install
内容的提问来源于stack exchange,提问作者Codex
相关产品推荐
相关产品推荐

