- 尝试在你的
tsconfig.json中将compilerOptions.module设置为CommonJS
- 不要将
"type": "module" 放入package.json
概念证明:
https://github.com/ferdinandant/stacko-unknown-ext-tsx
tsconfig.json
{
"include": ["src", "types/**/*"],
"exclude": ["node_modules/**"],
"compilerOptions": {
"module": "CommonJS",
"lib": ["dom", "esnext"],
"rootDir": "./src",
"jsx": "react",
"esModuleInterop": true,
"skipLibCheck": true,
"noEmit": true
},
// https://stackoverflow.com/questions/51610583/ts-node-ignores-d-ts-files-while-tsc-successfully-compiles-the-project
"ts-node": {
"files": true
}
}
package.json
{
"name": "stacko-unknown-ext-tsx",
"version": "1.0.0",
"main": "src/index.tsx",
"author": "Ferdinand Antonius",
"license": "MIT",
"scripts": {
"dev": "ts-node src/index.tsx"
},
"devDependencies": {
"@types/node": "^18.11.18",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
}
}
src/index.tsx
import fs from "fs";
console.log("Hello!");
console.log(fs);
以yarn ts-node src/index.tsx运行