joi-to-typescript icon indicating copy to clipboard operation
joi-to-typescript copied to clipboard

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

Open kingmesal opened this issue 2 years ago • 10 comments

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?

kingmesal avatar May 13 '22 20:05 kingmesal

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

mrjono1 avatar Jul 05 '22 09:07 mrjono1

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?

luiskugel avatar Jun 01 '23 12:06 luiskugel

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

mrjono1 avatar Jun 01 '23 23:06 mrjono1

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.

luiskugel avatar Jun 07 '23 18:06 luiskugel

awesome thankyou, there will be many people thanking you

mrjono1 avatar Jun 07 '23 23:06 mrjono1

updates?

Pnlvfx avatar Sep 29 '23 00:09 Pnlvfx

Im happy for this project to move to esm only

mrjono1 avatar Oct 13 '23 05:10 mrjono1

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)

throrin19 avatar Dec 28 '23 08:12 throrin19