我有一个电子应用程序,主要是所有的javascript,我也有一个express服务器项目,这是写在typescript,如果我试图使这个typescript项目的子进程在我的electron.js文件,我得到typescript错误,我的server.js不理解它试图导入的. ts文件类型(它们存在,它只是不知道如何处理它们)。TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
====电子应用程序===
下面是我在electron应用程序中的package.json:
"scripts": {
"build": "react-app-rewired build",
"start": "react-app-rewired start",
"electron": "electron ."
}
tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true
},
"include": ["src", "expressproject/src"]
}
下面是我在electron.js中运行express项目的方法:
//electron.js
const { spawn } = require("child_process");
// start express server
const expressServerProcess = spawn("node", [pathToServerJS]); // D:/expressproject/src/server.js
function createWindow() {
...
}
====快递项目====
下面是我的typescript express项目中的package.json
"scripts": {
"build": "tsc -p tsconfig.json",
"start": "npm run build && node --loader ts-node/esm --experimental-specifier-resolution=node server.js",
}
tsconfig.json
{
"extends": "ts-node/node16/tsconfig.json",
"ts-node": {
"transpileOnly": true,
"esm": true,
"compilerOptions": {
"outDir": "dist"
}
},
"compilerOptions": {
"outDir": "dist",
"target": "ESNext",
"module": "ES2020",
"lib": ["ES2020", "ES2021"],
"removeComments": true,
"strict": true,
"allowJs": true,
"skipLibCheck": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"sourceMap": true,
"stripInternal": true,
"declaration": true,
"noImplicitAny": true,
"allowUnusedLabels": false,
"allowUnreachableCode": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"useUnknownInCatchVariables": true,
"alwaysStrict": true,
"noImplicitReturns": true
}
}
我认为正在发生的是TypeScript代码没有被编译成JavaScript?我该怎么做?
1条答案
按热度按时间oprakyz71#
在基思的帮助下回答:
基本上传递我在typescript项目上的npm start脚本中使用的所有值(除了构建脚本),作为我在electron应用程序中的child_process中运行的命令。
typescript express server package.json:
node --loader ts-node/esm --experimental-specifier-resolution=node server.js
electron.js