nuxtjs3 - Nuxt 3 未捕获引用错误 : require is not define

标签 nuxtjs3

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)以来,importexport 成为了新的处理标准。 Nuxt 也遵循 ES

ES6 无法识别

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/

相关文章:

javascript - 500窗口未定义nuxt3和composition api

Firebase 功能多个目录 - Nuxt 3/Nitro

javascript - 如何从 Nuxt 3 服务器访问 httpOnly cookie

javascript - Nuxt.js3 插件的上下文变得未定义

javascript - 如何在nuxtjs中执行所有DOM加载后运行的函数?

nuxtjs3 - 在 Nuxt 3 中使用 Pinia 保持状态

Nuxt 3 中的 Axios 插件

typescript - 类型 'NuxtConfig' 中不存在“runtimeConfig”

unit-testing - 如何在 Nuxt 3 中用 vitest 为组件编写单元测试?

macos - MAC 和 Linux 中的 NUXT3 错误意外 token '||='