kev*_*vin 2 heroku node.js express typescript
- TSError: \xe2\xa8\xaf Unable to compile TypeScript:\n- src/server.ts(1,38): error TS7016: Could not find a declaration file for module \'express\'. \'/app/node_modules/express/index.js\' implicitly has an \'any\' type.\n- Try `npm i --save-dev @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module \'express\';`\n- src/server.ts(10,14): error TS2580: Cannot find name \'process\'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.\n
Run Code Online (Sandbox Code Playgroud)\n试图弄清楚如何让我的代码达到 Heroku 的状态已经被耽搁了一段时间,最新的错误如上面所示,我不确定需要更改什么才能使其正常工作。我一直在遵循指南,并在我的 package.json 脚本中有@types/express
、等。@types/node
然而,它并没有读取它们。不知道从这里去哪里,我们将不胜感激。
- TSError: \xe2\xa8\xaf Unable to compile TypeScript:\n- src/server.ts(1,38): error TS7016: Could not find a declaration file for module \'express\'. \'/app/node_modules/express/index.js\' implicitly has an \'any\' type.\n- Try `npm i --save-dev @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module \'express\';`\n- src/server.ts(10,14): error TS2580: Cannot find name \'process\'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.\n
Run Code Online (Sandbox Code Playgroud)\nweb:ts-node/src/server.ts \n
Run Code Online (Sandbox Code Playgroud)\nimport express, { Application } from \'express\';\nimport { routes } from \'./routes\';\n\n// Boot express\nexport const app: Application = express();\n\n// Application routing\nroutes(app);\n\nconst port = process.env.PORT || 5000;\n\n// Start server\napp.listen(port, () => console.log(`Server is listening on port ${port}!`));\n
Run Code Online (Sandbox Code Playgroud)\n{\n "compilerOptions": {\n "target": "es2017",\n "module": "commonjs",\n "sourceMap": true,\n "outDir": "./build",\n "rootDir": "./src",\n "strict": true,\n "noImplicitAny": true,\n "moduleResolution": "node",\n "baseUrl": "./src",\n "esModuleInterop": true,\n "skipLibCheck": true,\n "forceConsistentCasingInFileNames": true\n },\n "lib": ["es2015"],\n "include": ["src/**/*.ts"],\n "exclude": ["node_modules", "src/**/*.test.ts"]\n}\n
Run Code Online (Sandbox Code Playgroud)\n
弄清楚了。
Heroku 在构建项目时会修剪开发依赖项。由于我的所有@types
位置都devDependencies
无法访问,因此导致了最初的帖子中显示的错误。
// i also added engine specifications.
// the docs suggest matching your configurations as closely to your dev
// environment as possible for obvious reasons.
"engines": {
"node": "15.x",
"npm": "7.x"
},
...
"dependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/node": "^16.7.6",
"axios": "^0.21.1",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"ts-node": "^10.2.1",
"typescript": "^4.4.2"
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
882 次 |
最近记录: |