Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kingmesal opened this issue May 13, 2022 · 8 comments
Open

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

kingmesal opened this issue May 13, 2022 · 8 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@kingmesal
Copy link

kingmesal commented May 13, 2022

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 mrjono1 added the bug Something isn't working label Jul 5, 2022
@mrjono1
Copy link
Owner

mrjono1 commented Jul 5, 2022

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

@luiskugel
Copy link

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
Copy link
Owner

mrjono1 commented Jun 1, 2023

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

@luiskugel
Copy link

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
Copy link
Owner

mrjono1 commented Jun 7, 2023

awesome thankyou, there will be many people thanking you

@Pnlvfx
Copy link

Pnlvfx commented Sep 29, 2023

updates?

@mrjono1 mrjono1 added the help wanted Extra attention is needed label Oct 13, 2023
@mrjono1
Copy link
Owner

mrjono1 commented Oct 13, 2023

Im happy for this project to move to esm only

@throrin19
Copy link

throrin19 commented Dec 28, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants