如何将用 typescript 编写的 MERN 应用程序部署到 Heroku?
How can I deploy a MERN app written in typescrtipt to Heroku?
我找不到说明如何做我想做的事情的教程,我尝试混合几个做类似事情的教程,但无济于事。我有一个用 typescript 编写的 express 服务器和一个用 typescript 编写的 react 应用程序 运行 与它们在本地完全一样,但我无法将它们部署到 Heroku。 运行ning tsc post-install 时构建失败,错误是“找不到模块”和“没有 --jsx 标志就无法使用 jsx”的混合错误。我不确定到底需要改变什么才能实现它。
没有安装依赖项作为开发依赖项。 运行 tsc 本地显示没有错误。
我的package.json:
{
"name": "inventory",
"version": "1.0.0",
"description": "Odin Project inventory project",
"main": "source/server.ts",
"scripts": {
"client-install": "cd client && npm install",
"start": "node dist/source/server.js",
"server": "cd source && nodemon server.ts",
"client": "cd client && npm start",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"postinstall": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/JonathanDPotter/inventory.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/JonathanDPotter/inventory/issues"
},
"homepage": "https://github.com/JonathanDPotter/inventory#readme",
"dependencies": {
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.13",
"@types/mongoose": "^5.11.97",
"@types/react-router-dom": "^5.3.2",
"concurrently": "^6.4.0",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongoose": "^6.0.14",
"react-router-dom": "^6.2.1",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
},
"engines": {
"node": "16.13.1",
"npm": "7.12.1"
}
}
完整的构建日志很长,但这里有一个示例:
Try npm i --save-dev @types/react-dom
if it exists or add a new
declaration (.d.ts) file containing declare module 'react-dom';
client/src/index.tsx(3,26): error TS2307: Cannot find module 'react-redux' or its corresponding type declarations.
client/src/index.tsx(6,17): error TS6142: Module './App' was resolved to '/tmp/build_db5fb0f4/client/src/App.tsx', but '--jsx' is
not set.
client/src/index.tsx(9,3): error TS17004: Cannot use JSX unless the '--jsx' flag is provided.
client/src/index.tsx(10,5): error TS17004: Cannot use JSX unless the '--jsx' flag is provided.
client/src/index.tsx(11,7): error TS17004: Cannot use JSX unless the '--jsx' flag is provided.
client/src/store/index.ts(6,8): error TS2307: Cannot find module '@reduxjs/toolkit' or its corresponding type declarations.
Link 到 GitHub 回购 here.
我通过添加解决了这个问题:
"exclude": ["client", "node_modules", "public"]
到 tsconfig.json compilerOptions 之后(不在 compilerOptions 中)。
问题的根源在于 tsc 命令也试图编译客户端文件夹中的所有 React 应用程序。现在它构建完美。
我找不到说明如何做我想做的事情的教程,我尝试混合几个做类似事情的教程,但无济于事。我有一个用 typescript 编写的 express 服务器和一个用 typescript 编写的 react 应用程序 运行 与它们在本地完全一样,但我无法将它们部署到 Heroku。 运行ning tsc post-install 时构建失败,错误是“找不到模块”和“没有 --jsx 标志就无法使用 jsx”的混合错误。我不确定到底需要改变什么才能实现它。
没有安装依赖项作为开发依赖项。 运行 tsc 本地显示没有错误。
我的package.json:
{
"name": "inventory",
"version": "1.0.0",
"description": "Odin Project inventory project",
"main": "source/server.ts",
"scripts": {
"client-install": "cd client && npm install",
"start": "node dist/source/server.js",
"server": "cd source && nodemon server.ts",
"client": "cd client && npm start",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"postinstall": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/JonathanDPotter/inventory.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/JonathanDPotter/inventory/issues"
},
"homepage": "https://github.com/JonathanDPotter/inventory#readme",
"dependencies": {
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.13",
"@types/mongoose": "^5.11.97",
"@types/react-router-dom": "^5.3.2",
"concurrently": "^6.4.0",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongoose": "^6.0.14",
"react-router-dom": "^6.2.1",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
},
"engines": {
"node": "16.13.1",
"npm": "7.12.1"
}
}
完整的构建日志很长,但这里有一个示例:
Try
npm i --save-dev @types/react-dom
if it exists or add a new declaration (.d.ts) file containingdeclare module 'react-dom';
client/src/index.tsx(3,26): error TS2307: Cannot find module 'react-redux' or its corresponding type declarations. client/src/index.tsx(6,17): error TS6142: Module './App' was resolved to '/tmp/build_db5fb0f4/client/src/App.tsx', but '--jsx' is not set. client/src/index.tsx(9,3): error TS17004: Cannot use JSX unless the '--jsx' flag is provided. client/src/index.tsx(10,5): error TS17004: Cannot use JSX unless the '--jsx' flag is provided. client/src/index.tsx(11,7): error TS17004: Cannot use JSX unless the '--jsx' flag is provided. client/src/store/index.ts(6,8): error TS2307: Cannot find module '@reduxjs/toolkit' or its corresponding type declarations.
Link 到 GitHub 回购 here.
我通过添加解决了这个问题:
"exclude": ["client", "node_modules", "public"]
到 tsconfig.json compilerOptions 之后(不在 compilerOptions 中)。
问题的根源在于 tsc 命令也试图编译客户端文件夹中的所有 React 应用程序。现在它构建完美。