javascript - Nodejs API 部署到 Heroku
问题描述
我花了 2 天时间试图将这个 Nodejs API 部署到 Heroku(讽刺/笑话:它在我的机器上运行!相信我!哈哈)。我以前有很多很多错误,但我显然都修复了它们,现在除了这个:
2021-08-12T02:33:53.000000+00:00 app[api]: Build started by user xxxx@hotmail.com
2021-08-12T02:34:31.000000+00:00 app[api]: Build succeeded
2021-08-12T02:34:31.051122+00:00 app[api]: Deploy 51176bf9 by user xxxxx@hotmail.com
2021-08-12T02:34:31.051122+00:00 app[api]: Release v38 created by user xxxxxx@hotmail.com
2021-08-12T02:34:32.043979+00:00 heroku[web.1]: State changed from crashed to starting
2021-08-12T02:34:36.299607+00:00 heroku[web.1]: Starting process with command `npm start`
2021-08-12T02:34:40.689105+00:00 app[web.1]:
2021-08-12T02:34:40.689147+00:00 app[web.1]: > backend@1.0.0 start /app
2021-08-12T02:34:40.689147+00:00 app[web.1]: > node ./dist/index.js
2021-08-12T02:34:40.689148+00:00 app[web.1]:
2021-08-12T02:34:40.925689+00:00 app[web.1]: file:///app/dist/index.js:5
2021-08-12T02:34:40.925709+00:00 app[web.1]: Object.defineProperty(exports, "__esModule", { value: true });
2021-08-12T02:34:40.925710+00:00 app[web.1]: ^
2021-08-12T02:34:40.925711+00:00 app[web.1]:
2021-08-12T02:34:40.925711+00:00 app[web.1]: ReferenceError: exports is not defined in ES module scope
2021-08-12T02:34:40.925717+00:00 app[web.1]: This file is being treated as an ES module because it has a '.js' file extension and '/app/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
2021-08-12T02:34:40.925721+00:00 app[web.1]: at file:///app/dist/index.js:5:23
2021-08-12T02:34:40.925726+00:00 app[web.1]: at ModuleJob.run (internal/modules/esm/module_job.js:170:25)
2021-08-12T02:34:40.925727+00:00 app[web.1]: at async Loader.import (internal/modules/esm/loader.js:178:24)
2021-08-12T02:34:40.925727+00:00 app[web.1]: at async Object.loadESM (internal/process/esm_loader.js:68:5)
2021-08-12T02:34:40.978104+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-08-12T02:34:40.983071+00:00 app[web.1]: npm ERR! errno 1
2021-08-12T02:34:40.992651+00:00 app[web.1]: npm ERR! backend@1.0.0 start: `node ./dist/index.js`
2021-08-12T02:34:40.992928+00:00 app[web.1]: npm ERR! Exit status 1
2021-08-12T02:34:40.993242+00:00 app[web.1]: npm ERR!
2021-08-12T02:34:40.993515+00:00 app[web.1]: npm ERR! Failed at the backend@1.0.0 start script.
2021-08-12T02:34:40.993715+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-08-12T02:34:41.004082+00:00 app[web.1]:
2021-08-12T02:34:41.004085+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-08-12T02:34:41.004086+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-08-12T02_34_40_994Z-debug.log
2021-08-12T02:34:41.074430+00:00 heroku[web.1]: Process exited with status 1
2021-08-12T02:34:41.154351+00:00 heroku[web.1]: State changed from starting to crashed
我已经尝试了很多东西,但据我所知,我不能使用这个建议的解决方案,因为我正在使用 Typescript 并使用“import (...)”,所以我需要保留"type": "module"
和"module": "commonjs"
...
这是我当前的 package.json:
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "./src/server.ts",
"type": "module",
"scripts": {
"build": "rimraf dist && tsc",
"dev": "ts-node-dev --respawn --pretty --transpile-only ./src/index.ts",
"start": "node ./dist/index.js"
},
"engines": {
"node": "14.x",
"npm": "6.x"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/body-parser": "^1.19.1",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"cors": "^2.8.5",
"express": "^4.17.1",
"helmet": "^4.6.0",
"rimraf": "^3.0.2",
"save": "^2.4.0",
"ts-node": "^10.1.0",
"ts-node-dev": "^1.1.8"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/helmet": "^4.0.0",
"@types/node": "^16.4.13",
"@typescript-eslint/eslint-plugin": "^3.9.1",
"@typescript-eslint/parser": "^3.8.0",
"dotenv": "^10.0.0",
"typescript": "^4.3.5"
}
}
这是我当前的 tsconfig.json:
{
"compilerOptions": { /* Enable incremental compilation */
"target": "ESNEXT", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"outDir": "dist", /* Redirect output structure to the directory. */
"strict": true, /* Enable all strict type-checking options. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
这是我第一次部署到 Heroku,但我从官方文档、论坛中阅读了很多内容,但似乎没有任何效果......
我正在使用连接到 Heroku 的 Github,这种集成似乎运行良好。
哦,这是我的 index.ts:
import dotenv from "dotenv";
import cors from "cors";
import helmet from 'helmet';
import express from "express";
import portfolioRouter from "./routes/portfolio-routes";
import teamRouter from "./routes/team-routes";
dotenv.config();
let PORT = parseInt(process.env.PORT as string, 10);
console.log("PORT: ", PORT);
const app = express();
app.use(cors());
app.use(helmet());
app.use(express.json())
app.use(express.urlencoded({ extended: true}))
app.use(express.json());
app.use("/api/v1/portfolio", portfolioRouter);
app.use("/api/v1/team", teamRouter);
console.log("PORT being used: ", PORT);
app.listen(PORT || 8080, () => {
console.log(`Listening on port ${PORT}`);
});
任何人?谢谢!
解决方案
尝试设置"module":"commonjs"
为"module":"es6"
推荐阅读
- javascript - 如何对多个数组做出反应
- http - http.TimeoutHandler 返回但 handlerfunc 继续运行
- node.js - 如何使用节点 js 对 mongoDB 中的集合内的数组进行排序
- java - Swagger 所需参数在丢失时返回 500 而不是 404
- reactjs - 不调用重定向回调,但访问了重定向 URI
- php - php 语法我在绑定参数和 SELECT 时遇到错误
- java - 如何处理陈旧的元素引用:元素未附加到页面文档
- arrays - 谷歌电子表格公式 - SUMIFS - 坏了?
- jersey - 异常映射器的控制 Content-Type 标头
- ansible - ansible模板使用变量和字符串