fix ts-node esm error All In One
error ❌
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
ESM solution ✅
"type": "module",
&ts-node-esm
{
"name": "patch-package-in-action",
"version": "1.0.0",
"description": "patch-package in action",
"main": "./src/index.ts",
"type": "module",
"scripts": {
"dev-esm": "npx ts-node-esm ./src/index.ts",
"app-esm": "npx ts-node-esm ./src/app.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/web-fullstack/patch-package-in-action.git"
},
"author": "xgqfrms",
"license": "MIT",
"dependencies": {
"lodash-es": "^4.17.21",
"typescript": "^5.0.2"
},
"devDependencies": {
"@types/lodash-es": "^4.17.7",
"app-node-env": "^1.4.7",
"rimraf": "^4.4.0",
"ts-node": "^10.9.1"
}
}
esm
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"ts-node": {
"esm": true
},
}
lodash-es
import {toString} from "lodash-es";
type Obj = {
[key: string]: any;
}
const obj: Obj = {
name: 'xgqfrms',
country: 'China',
language: 'zh-hans',
};
const str = toString(obj);
console.log(`toString =`, str1);
https://github.com/web-fullstack/patch-package-in-action