joi icon indicating copy to clipboard operation
joi copied to clipboard

Difficulties when importing, ... is not a function

Open sezanzeb opened this issue 1 year ago • 0 comments

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

"allowSyntheticDefaultImports": false,
"esModuleInterop": true,

in tsconfig.json and

import Joi from 'joi';

causes

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


Using

"allowSyntheticDefaultImports": false,
"esModuleInterop": true,

in tsconfig.json and

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

causes

TypeError: Joi.string is not a function


Using

"allowSyntheticDefaultImports": true,
"esModuleInterop": true,

in tsconfig.json and

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

causes

TypeError: Joi.string is not a function


Using

"allowSyntheticDefaultImports": true,
"esModuleInterop": false,

in tsconfig.json and

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

"allowSyntheticDefaultImports": true,
"esModuleInterop": true,

in tsconfig.json and

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

sezanzeb avatar Dec 19 '22 10:12 sezanzeb