joi-to-typescript
joi-to-typescript copied to clipboard
Package.json -> type: "module" -- not supported
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?
its sounding like I should drop commonjs module and just go esm
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?
yes, if you have time to create a PR to fix this that would be awesome
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.
awesome thankyou, there will be many people thanking you
updates?
Im happy for this project to move to esm only
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)