问题描述
我正在运行一个应用程序,该应用程序在本地运行时没有任何错误,但是当我在Heroku上部署它时,它部署并最终导致应用程序崩溃。
我的package.json。
{
"name": "app-server","main": "index.ts","scripts": {
"start": "nodemon index.ts"
},"engines": {
"node": "10.16.x","npm": "6.x"
},"license": "ISC","dependencies": {
"@types/mongoose": "^5.7.7","@types/node": "^13.9.2","@types/react-router-dom": "^5.1.3","config": "^3.3.2","cors": "^2.8.5","express": "^4.17.1","jsonwebtoken": "^8.5.1","moment": "^2.29.1","mongodb": "^3.5.5","mongoose": "^5.10.9","nodemon": "^2.0.5","react-router-dom": "^5.1.2","socket.io": "^2.3.0","ts-node": "^8.10.2","typescript": "^3.9.7"
},"devDependencies": {
"@types/config": "0.0.36","@types/cors": "^2.8.6","@types/express": "^4.17.3","@types/jsonwebtoken": "^8.3.9"
}
}
我的tsconfig.json
{
"compilerOptions": {
"target": "es2018","module": "commonjs","lib": ["es2018","esnext.asynciterable"],"skipLibCheck": true,"strict": true,"strictFunctionTypes": false,"strictPropertyInitialization": false,"esModuleInterop": true,"experimentalDecorators": true,"emitDecoratorMetadata": true
}
}
web: npm start
Heroku的构建日志
Node.js app detected
-----> Creating runtime environment
NPM_CONfig_LOGLEVEL=error
NODE_ENV=production
NODE_MODULES_CACHE=true
NODE_VERBOSE=false
-----> Installing binaries
engines.node (package.json): 10.16.x
engines.npm (package.json): 6.x
Resolving node version 10.16.x...
Downloading and installing node 10.16.3...
Bootstrapping npm 6.x (replacing 6.9.0)...
npm 6.x installed
-----> Restoring cache
- node_modules
-----> Installing dependencies
Installing node modules
> nodemon@2.0.5 postinstall
> node bin/postinstall || exit 0
-----> Build
-----> Caching build
- node_modules
-----> Pruning devDependencies
removed 14 packages and audited 295 packages in 2.153s
-----> Build succeeded!
-----> discovering process types
procfile declares types -> web
-----> Launching...
Heroku的错误日志
2020-11-04T09:55:14.920223+00:00 app[web.1]: /app/node_modules/ts-node/src/index.ts:434
2020-11-04T09:55:14.920224+00:00 app[web.1]: return new TSError(diagnosticText,diagnosticCodes)
2020-11-04T09:55:14.920225+00:00 app[web.1]: ^
2020-11-04T09:55:14.920420+00:00 app[web.1]: TSError: ⨯ Unable to compile TypeScript:
2020-11-04T09:55:14.920422+00:00 app[web.1]: index.ts(1,21): error TS7016: Could not find a declaration file for module 'express'. '/app/node_modules/express/index.js' implicitly has an 'any' type.
2020-11-04T09:55:14.920422+00:00 app[web.1]: Try `npm install @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module 'express';`
2020-11-04T09:55:14.920423+00:00 app[web.1]: index.ts(8,20): error TS7016: Could not find a declaration file for module 'config'. '/app/node_modules/config/lib/config.js' implicitly has an 'any' type.
2020-11-04T09:55:14.920423+00:00 app[web.1]: Try `npm install @types/config` if it exists or add a new declaration (.d.ts) file containing `declare module 'config';`
2020-11-04T09:55:14.920424+00:00 app[web.1]: index.ts(9,18): error TS7016: Could not find a declaration file for module 'cors'. '/app/node_modules/cors/lib/index.js' implicitly has an 'any' type.
2020-11-04T09:55:14.920424+00:00 app[web.1]: Try `npm install @types/cors` if it exists or add a new declaration (.d.ts) file containing `declare module 'cors';`
2020-11-04T09:55:14.920425+00:00 app[web.1]:
任何人,请让我知道为了正确部署需要更改哪些配置
解决方法
首先,Heroku剥离了dev依赖关系-这就是为什么您的部署无法正常工作的原因。您可以将所有内容移至依赖项以解决该问题-但我确实不推荐这样做。
您应该从Running "clean:dirs" (clean) task
>> 0 paths cleaned.
Running "tslint:plugin" (tslint) task
>> 6 files lint free.
Running "shell:command" (shell) task
node_modules/@ephox/agar/lib/main/ts/ephox/agar/api/DragnDrop.d.ts(11,34): error TS2304: Cannot find name 'File'.
node_modules/@ephox/agar/lib/main/ts/ephox/agar/api/DragnDrop.d.ts(15,38): error TS2304: Cannot find name 'File'.
// A lot other node_modules error lines here
node_modules/tinymce/tinymce.d.ts(2745,29): error TS2304: Cannot find name 'Document'.
node_modules/tinymce/tinymce.d.ts(2751,45): error TS2304: Cannot find name 'Window'.
node_modules/tinymce/tinymce.d.ts(2752,54): error TS2304: Cannot find name 'Document'.
node_modules/tinymce/tinymce.d.ts(2752,65): error TS2304: Cannot find name 'ShadowRoot'.
Warning: Command failed: tsc
Use --force to continue.
Aborted due to warnings.
npm ERR! code ELIFECYCLE
npm ERR! errno 6
npm ERR! myproject@1.0.0 build: `grunt`
npm ERR! Exit status 6
npm ERR!
npm ERR! Failed at the myproject@1.0.0 build script.
命令中删除nodemon
并使用start
。但是要使其正常工作,您需要将打字稿编译为javascript。您可以使用node index.js
命令来完成它。
总结,您需要:
- 创建一个运行
tsc
的构建命令。
tsc
您也可以使用
{ "scripts": { "build": "tsc" } }
代替heroku调用的build。
- 更新启动命令以使用
postinstall
代替JS
。
TS