Nuxt3项目
npm 运行构建
npm 运行开始
Nuxi 3.0.0 15:16:13
ℹ Node.js version: 14.21.2 15:16:13
ℹ Preset: node-server 15:16:13
ℹ Working dir: .output 15:16:13
ℹ Loading .env. This will not be loaded when running the server in production. 15:16:13
ℹ Starting preview command: node ./server/index.mjs 15:16:13
15:16:13
Listening http://[::]:3000
http://localhost:3000/
然后浏览器出现错误
entry.042ab8e0.js:19 Uncaught ReferenceError: require is not defined
at entry.042ab8e0.js:19:3018
将我的节点 14 升级到 18 然后仍然遇到相同的错误
最佳答案
简短回答:ordered-uuid
库设计用于“传统”Node 后端而不是前端。
更长的答案:require
是来自 CommonJS
的东西,这是 NodeJs 使用的东西。它被用作动态导入模块的一种方式。然而,自从 ES6 中引入 JavaScript 模块(又名 JS 2015、ECMAScript 2015、ECMAScript 6)以来,import
和 export
成为了新的处理标准。 Nuxt 也遵循 ES
require
,这也是您收到此错误的原因。由于 ordered-uuid
在过去 6 年里没有更新过。我建议你找到更适合你需求的东西。例如:https://github.com/danielboven/ordered-uuid-v4
npm install ordered-uuid-v4
该库同时具有 CommonJS 兼容方法和 ES6 兼容方法。
Nuxt 实际上有一个很好的页面,深入解释了有关 CommonJS、ES 模块的所有内容:看一下 here .
关于nuxtjs3 - Nuxt 3 未捕获引用错误 : require is not define,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74980947/