joi icon indicating copy to clipboard operation
joi copied to clipboard

Is JOI usable in browser?

Open DaveStein opened this issue 2 years ago • 4 comments

Support plan

  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: N/A
  • module version: 17.7.0
  • environment (e.g. node, browser, native): browser (just TSC compilation)
  • used with (e.g. hapi application, another framework, standalone, ...): in an Open WC generator project
  • any other relevant information:

How can we help?

I have used JOI previously on my Node apps. I am now doing something client side, although I will hit server endpoints. I want to share the validation across both. I do import Joi from 'joi'; after turning on esModuleInterop in TS config, as it told me. In the browser I get this error:

The requested module './../../../node_modules/joi/lib/index.js' does not provide an export named 'default'

I saw elsewhere that this is supposed to work in React create app, but that uses Webpack. Is that a requirement of using this package in browser??

DaveStein avatar Jan 31 '23 03:01 DaveStein

yes, it's supported(if your bundler was set correctly). see https://github.com/hapijs/joi/blob/b23659d9f57ad420a5c9a2cf399bd5e01d1f6501/package.json#L8

another workaround is to import the browser version manually:

import joi from 'joi/dist/joi-browser.min.js';

aladdin-add avatar Jan 31 '23 03:01 aladdin-add

@aladdin-add I am not using a bundler for dev. I am using Open WC https://open-wc.org/docs/development/generator/, which uses Rollup for build, but not for dev. For dev it simply does tsc && concurrently -k -r \"tsc --watch --preserveWatchOutput\" \"wds\"

I tried doing the min workaround directly a little before you reply and got this issue. I know when I tried adding types earlier, the installation said I shouldn't need to do that since the package comes with types.

image

DaveStein avatar Jan 31 '23 03:01 DaveStein

Have you tried to import joi as below?

import { joi } from "joi";

In a past project I was able to use it in a frontend project but there was a bundler involved though.

Nargonath avatar Jan 31 '23 12:01 Nargonath

@Nargonath nope... it errors saying i must use default so kind of a catch-22.

DaveStein avatar Jan 31 '23 15:01 DaveStein