hapijs / joi

The most powerful data validation library for JS
Other
20.93k stars 1.51k forks source link

Nuxt 3.7.4 + Joi 17.11.0 not functioning correctly in the built project #2989

Closed yongjie-zhang-mail closed 7 months ago

yongjie-zhang-mail commented 1 year ago

In my project, I'm using Nuxt 3.7.4 and Joi 17.11.0 for parameter validation. Everything works fine in development mode, but when I build the project and run it in a Node.js environment, I encounter the following error:

[nuxt] [request error] [unhandled] [500] Cannot find module 'C:\Users\tester\Desktop\workspace\nuxt3\One\.output\server\node_modules\@hapi\hoek\lib\assert' imported from C:\Users\tester\Desktop\workspace\nuxt3\One\.output\server\chunks\_id_.get.mjs
Did you mean to import @hapi/hoek/lib/assert.js?

Reproducible Steps:

In a Node.js environment with version 18.15.0, I created a Nuxt 3 project using the command npx nuxi init <project_name> and added the Joi dependency with yarn add joi.

I added a file server\api\user[id].get.ts with the following content:

import Joi from "joi"

export default defineEventHandler(async (event) => {
    const params = getRouterParams(event)
    const schema = Joi.object({
        id: Joi.number().required().label("params.id"),
    });
    const res = await schema.validateAsync(params);
    return res
})

I ran yarn run dev and sent a request to http://localhost:3000/api/user/123, which resulted in the following correct response:

{
  "id": 123
}

I then executed yarn run build and node .output/server/index.mjs and attempted to send a request to http://localhost:3000/api/user/123, but received the following error:

Listening on http://[::]:3000
[nuxt] [request error] [unhandled] [500] Cannot find module 'C:\Users\tester\Desktop\workspace\nuxt3\One\.output\server\node_modules\@hapi\hoek\lib\assert' imported from C:\Users\tester\Desktop\workspace\nuxt3\One\.output\server\chunks\_id_.get.mjs
Did you mean to import @hapi/hoek/lib/assert.js?
  at new NodeError (node:internal/errors:399:5)
  at finalizeResolution (node:internal/modules/esm/resolve:326:11)
  at moduleResolve (node:internal/modules/esm/resolve:945:10)
  at defaultResolve (node:internal/modules/esm/resolve:1153:11)
  at nextResolve (node:internal/modules/esm/loader:163:28)
  at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
  at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
  at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
  at link (node:internal/modules/esm/module_job:76:36)

Key Code Snippets:

package.json

{
  "name": "nuxt-app",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@nuxt/devtools": "latest",
    "nuxt": "^3.7.4",
    "vue": "^3.3.4",
    "vue-router": "^4.2.5"
  },
  "dependencies": {
    "joi": "^17.11.0"
  }
}

Actual Result: I encounter the aforementioned error and cannot run the built project as expected.

manniL commented 10 months ago

Can't repro this in latest nuxt + nitro. See the StackBlitz

sebaguse commented 8 months ago

I just experienced exactly the same issue. Joi works perfectly with dev server but doesn't with build. Just throws same error as above. I tried with:

Operating System: Windows_NT Node Version: v20.11.1 Nuxt Version: 3.10.2 CLI Version: 3.10.1 Nitro Version: 2.8.1 Package Manager: npm@10.2.4 Builder: -

User Config: app, runtimeConfig, css, modules, quasar, pinia, imports, vite, content, vue, nitro, devtools, devServer, ssr Runtime Modules: nuxt-quasar-ui@2.0.7, @pinia/nuxt@0.5.1, @vueuse/nuxt@10.7.2, @pinia-plugin-persistedstate/nuxt@1.2.0, @nuxt/content@2.12.0, @formkit/auto-animate/nuxt@0.8.1 Build Modules: -

Joi: 17.12.1

wonder if there's any proper solution for that problem?

sebaguse commented 8 months ago

I just experienced exactly the same issue. Joi works perfectly with dev server but doesn't with build. Just throws same error as above. I tried with:

Operating System: Windows_NT Node Version: v20.11.1 Nuxt Version: 3.10.2 CLI Version: 3.10.1 Nitro Version: 2.8.1 Package Manager: npm@10.2.4 Builder: -

User Config: app, runtimeConfig, css, modules, quasar, pinia, imports, vite, content, vue, nitro, devtools, devServer, ssr Runtime Modules: nuxt-quasar-ui@2.0.7, @pinia/nuxt@0.5.1, @vueuse/nuxt@10.7.2, @pinia-plugin-persistedstate/nuxt@1.2.0, @nuxt/content@2.12.0, @formkit/auto-animate/nuxt@0.8.1 Build Modules: -

Joi: 17.12.1

wonder if there's any proper solution for that problem?

never mind ;) I found this solution and it seems to work perfectly: https://github.com/nuxt/nuxt/discussions/23065#discussioncomment-6939569