mrjono1 / joi-to-typescript

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

Package.json -> type: "module" -- not supported #231

Open kingmesal opened 2 years ago

kingmesal commented 2 years ago

I recently needed to add type:"module" to my package.json and setup my modules: "ESNext". This has caused joi-to-typescript to break with the following error.

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /scripts/makeTypes.ts
    at new NodeError (node:internal/errors:372:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:56:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:83:38)
    at defaultLoad (node:internal/modules/esm/load:21:20)
    at ESMLoader.load (node:internal/modules/esm/loader:407:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:326:22)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:345:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:304:34)
    at async Promise.all (index 0) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Now, if I add this to my tsconfig.json:

    "ts-node": {
        "compilerOptions": {
            "module": "CommonJS"
        }
    },

and remove type: "module" from package.json

While I can clearly remove / readd the type module to my package.json, I was wondering if there is a build target fix that would resolve this... e.g. not commonjs module.

Any thoughts?

mrjono1 commented 2 years ago

its sounding like I should drop commonjs module and just go esm

luiskugel commented 1 year ago

Hi, I just wanted to ask if there was an update on this. I would really like to use this package, but I have to use esm. So changing back to cjs is not an option for me.

Is there any way to help you with this?

mrjono1 commented 1 year ago

yes, if you have time to create a PR to fix this that would be awesome

luiskugel commented 1 year ago

Ok, I just have to get a release out the next week. When that is done in about 2 weeks, I will make the pull request.

I have already forked and implemented it, just have to make it nice and create the pull request.

mrjono1 commented 1 year ago

awesome thankyou, there will be many people thanking you

Pnlvfx commented 11 months ago

updates?

mrjono1 commented 11 months ago

Im happy for this project to move to esm only

throrin19 commented 8 months ago

Any news about this ? I have same problem on my project I moved to full ESM.

This is my joiToTypescript.ts config file :

import joiToTS from 'joi-to-typescript';

const types = async (): Promise<void> => {
    // eslint-disable-next-line no-console
    console.log('Running joi-to-typescript...');

    // Configure your settings here
    const result = await joiToTS.convertFromDirectory({
        schemaDirectory     : './src/schemas',
        typeOutputDirectory : './src/types',
        debug               : true,
        indexAllToRoot      : false,
        ignoreFiles         : [
            'index.ts',
        ],
    });

    if (result) {
        // eslint-disable-next-line no-console
        console.log('Completed joi-to-typescript');
    } else {
        // eslint-disable-next-line no-console
        console.log('Failed to run joi-to-typescript');
    }
};

types();

When I launch it in node 20 using node --loader ts-node/esm ../../commonConfiguration/joiToTypescript.t, I have this error :

(node:29354) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:29354) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/myproject/node_modules/joi-to-typescript/dist/module/index.js:1
import Path from 'path';
^^^^^^
SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at cjsLoader (node:internal/modules/esm/translators:345:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:294:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)