mrjono1 / joi-to-typescript

Convert Joi Schemas to TypeScript interfaces
MIT License
124 stars 39 forks source link

Error when import schema from barrel file #390

Closed throrin19 closed 7 months ago

throrin19 commented 8 months ago

Describe the bug

When I try to generate type of a schema, extend other schema imported from a barrel file, I have exotic error from unused file in the targeted schema but imported in the barrel file :

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /my-project/node_modules/@middy/http-error-handler/package.json
    at new NodeError (node:internal/errors:406:5)
    at exportsNotFound (node:internal/modules/esm/resolve:268:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:542:13)
    at resolveExports (node:internal/modules/cjs/loader:547:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:621:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1034:27)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/my-project/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
    at Function.Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

To Reproduce Steps to reproduce the behavior:

import { Joi } from '@aquassay/helpers';
import { paginationSchema } from '../../libs/middleware-validator';

const schema = paginationSchema.keys({
    hasRoute    : Joi.boolean().default(false).description('IF true : return all authTokens, including the new field dbMachines'),
    customerId : Joi.number().integer().optional().description('Customer ID'),
    orgId      : Joi.number().integer().optional().description('Org ID'),
    placeId    : Joi.string().uuid().optional().description('Place ID'),
    pointId    : Joi.string().uuid().optional().description('Point ID'),
}).meta({ className : 'authtokenList' });

export default schema;

And this is the barrel file ../../libs/middleware-validator/index.ts :

export * from './src/httpValidator'; // file with the module launch error
export * from './src/validate';
export * from './src/schemas'; // imported schema
export * from './src/types';

Expected behavior

I expect have not the error.

In notice, I have this error after passing to node 20.9 and typescript 5.3. Before, in node 18.x and typescript 5.2, I have no problems

And If I point directly to the schema file, I have no error. But I have to use the barrel file and to pass directly to it, not to directly point to schema file

cmaster11 commented 8 months ago

@throrin19 any chance this is related to https://github.com/middyjs/middy/commit/7c4cc5216a6cbf8f60dd6ea7f6011ab449a745b6 ? Which version of middy are you using? I see CJS in the loading path of your code and 3 weeks ago the v5 was releases dropping CJS support.

If that's the case, and you are using require calls, you need to instead use import calls.

Taken from another lib, an example of the migration path: https://github.com/ipfs/js-ipfs/blob/master/docs/upgrading/v0.62-v0.63.md#esm

throrin19 commented 7 months ago

Yep it's the cause. Very messy this migration between cjs/esm and typescript around all of it :/ .

Sorry for the disagreement