Description
Summary
In the website, ReadMe, and other docs, it says moduleType
: Override the module type of certain files, ignoring the package.json "type" field. See "Module type overrides" for details.. However moduleType
is an internal-only field, not to be used by the tsconfig.json's ts-node
field.
Expected Behavior
Any field listed in the website, ReadMe, etc. to be usable.
Actual Behavior
This field (to my knowledge, please correct me if I'm wrong) doesn't do anything. moduleTypes
(emphasis on the trailing s
) does.
Steps to reproduce the problem
Look through the source code linked above.
Alternatively, don't set type: module
in package.json, import some CJS file into your TS file, and then change the tsconfig.json given below from
// no errors
"moduleType": "esm",
"moduleTypes": {
"**/*.mjs": "esm",
"**/*.cjs": "cjs"
}
to
// lots of errors
"moduleTypes": {
"**/*.js": "esm",
"**/*.mjs": "esm",
"**/*.cjs": "cjs"
}
and watch ts-node
collapse.
Specifications
- ts-node version: v10.4.0 (latest)
- node version: v16.13.2
- TypeScript version: v4.6.0-dev.20220204 (though tested on 4.5.4 and 4.5.5 as well)
- tsconfig.json, if you're using one:
{
"compilerOptions": {
"target": "ESNext",
"lib": [
"ESNext",
"DOM",
"DOM.Iterable"
],
"module": "ESNext",
"moduleResolution": "node",
"strict": true,
"jsx": "react-jsx",
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
"newLine": "lf",
"preserveSymlinks": false,
"incremental": true,
"isolatedModules": true,
"outDir": "dist",
"declaration": true,
"declarationDir": "dist/types",
"baseUrl": ".",
"paths": {
"@/*": [ "src/*" ],
"/*": [ "*" ]
}
},
"include": [
"./src/**/*"
],
"exclude": [
"./node_modules",
"dist"
],
"ts-node": {
"require": [
"tsconfig-paths/register"
],
"preferTsExts": true,
"experimentalReplAwait": true,
"transpileOnly": true,
"moduleType": "esm", // Field in question
"moduleTypes": {
"**/*.mjs": "esm",
"**/*.cjs": "cjs"
},
"compilerOptions": {
"module": "nodenext",
"isolatedModules": false
},
"include": [
"./**/*"
]
}
}
- Operating system and version: Mac (Big Sur 11.6.1), Linux (Mint Uma)
Related
Maybe this is part of the experimental phase of ts-node
, but I had assumed moduleType: "esm"
would feign setting type: module
in package.json, but clearly it does not. In fact, it doesn't appear to do anything.
Suggested Fix
Either fix (what I assume to be) the typo in the docs, or (preferably) make moduleType
do as it currently says it does and override type
in package.json.