【发布时间】:2022-01-29 17:05:17
【问题描述】:
我有使用 react 和 nextjs 的 nodejs 应用程序。应用程序最重要的部分保存在服务器文件中。
main.js(服务器文件)
const app = next({ dev }) //dev = true
...
const DR_LINK = "anything"
module.exports.app = app;
module.exports.DR_LINK = DR_LINK;
现在我需要一些路由器的 app 对象。
//any router file
const {app} = require("../main")
而且它工作得非常好。 但是当我尝试导入字符串 DR_LINK 对象时
import {DR_LINK} from "../main";
//not in the same file as the import for app
我收到此错误
error - ./node_modules/destroy/index.js:14:0
Module not found: Can't resolve 'fs'
Import trace for requested module:
./node_modules/send/index.js
./node_modules/express/lib/response.js
./node_modules/express/lib/express.js
./node_modules/express/index.js
./main.js
./components/CreateForm.js
./pages/create.js
https://nextjs.org/docs/messages/module-not-found
error - unhandledRejection: OverwriteModelError: Cannot overwrite `Declaration` model once compiled.
//for some reason it affects the db models too
导出订单或导出之类的
module.exports = { app, DR_LINK};
没有区别。应用程序总是被导出,但字符串对象不是。这仅在以任何方式从服务器文件导出应用程序对象时发生。通过删除它,一切正常。
【问题讨论】:
-
您能否显示您要导入的文件的完整代码
DR_LINK? -
您的某个软件包似乎依赖于它无法找到的 fs。试试
npm install或npm ci。
标签: node.js next.js node-modules module.exports