hapijs / hapijs/joi

Difficulties when importing, ... is not a function

Open
#2,891 0 comments 3 reactions 0 assignees View on GitHub
support
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

#### Support plan

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

#### Context

* *node version*: 18.12.1
* *module version with issue*: 17.7.0
* *last module version without issue*: idk
* *environment* (e.g. node, browser, native): node, typescript
* *used with* (e.g. hapi application, another framework, standalone, ...): nest, jest
* *any other relevant information*:

#### What are you trying to achieve or the steps to reproduce?

Using

```json
"allowSyntheticDefaultImports": false,
"esModuleInterop": true,
```

in tsconfig.json and

```ts
import Joi from 'joi';
```

causes

`node_modules/joi/lib/index"' can only be default-imported using the 'allowSyntheticDefaultImports' flag`

---

Using

```json
"allowSyntheticDefaultImports": false,
"esModuleInterop": true,
```

in tsconfig.json and

```ts
import * as Joi from 'joi';
Joi.string()
```

causes

`TypeError: Joi.string is not a function`

---

Using

```json
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
```

in tsconfig.json and

```ts
import * as Joi from 'joi';
Joi.string()
```

causes

`TypeError: Joi.string is not a function`

---

Using

```json
"allowSyntheticDefaultImports": true,
"esModuleInterop": false,
```

in tsconfig.json and

```ts
import Joi from 'joi';
Joi.string()
```

works when running `nest start --exec "node --experimental-specifier-resolution=node --no-warnings` *(we always have to use that node option, it is not related to Joi and I didn't conduct experiments on how the Joy import behaves without it)*,
but jest tests fail with

```
● Test suite failed to run

TypeError: Cannot read properties of undefined (reading 'string')

3 | import Joi from 'joi';
4 | // import * as Joi from 'joi';
> 5 | Joi.string()
```

---

Using

```json
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
```

in tsconfig.json and

```ts
import Joi from 'joi';
Joi.string()
```

works everywhere.

In our package.json we use `"type": "module"`, I don't know if this is related to the problem.

#### What was the result you got?

see above

#### What result did you expect?

For it to work like shown in the nest js docs: https://docs.nestjs.com/techniques/configuration#schema-validation, with the `* as Joi` import without having to configure anything

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.