将Typescript和Node.js服务器部署到render.com

huangapple go评论65阅读模式
英文:

deploy Typescrpit+Node.js server to render.com

问题

I am trying to deploy my TS+Node.js to render.com and it shows:

Jun 26 01:51:02 PM  ==> Starting service with 'node app.ts' Jun 26 01:51:02 PM  internal/process/esm_loader.js:74 Jun 26 01:51:02 PM      internalBinding('errors').triggerUncaughtException( Jun 26 01:51:02 PM                                ^ Jun 26 01:51:02 PM   Jun 26 01:51:02 PM  TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /opt/render/project/src/app.ts Jun 26 01:51:02 PM      at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15) Jun 26 01:51:02 PM      at Loader.getFormat (internal/modules/esm/loader.js:104:42) Jun 26 01:51:02 PM      at Loader.getModuleJob (internal/modules/esm/loader.js:242:31) Jun 26 01:51:02 PM      at async Loader.import (internal/modules/esm/loader.js:176:17) Jun 26 01:51:02 PM      at async Object.loadESM (internal/process/esm_loader.js:68:5) { Jun 26 01:51:02 PM    code: 'ERR_UNKNOWN_FILE_EXTENSION' Jun 26 01:51:02 PM  }

This is my package.json:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "app.ts",
  "scripts": {
    "start": "ts-node-dev app.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/cookie-parser": "^1.4.3",
    "@types/cookie-session": "^2.0.44",
    "@types/cors": "^2.8.13",
    "@types/express": "^4.17.17",
    "@types/jsonwebtoken": "^9.0.1",
    "@types/node-cron": "^3.0.7",
    "express": "^4.18.2",
    "nodemon": "^2.0.22",
    "ts-node": "^10.9.1",
    "ts-node-dev": "^2.0.0",
    "typescript": "^5.0.3"
  },
  "dependencies": {
    "@sendgrid/mail": "^7.7.0",
    "@types/joi": "^17.2.3",
    "axios": "^1.4.0",
    "bcrypt": "^5.1.0",
    "cloudinary": "^1.37.0",
    "cookie-parser": "^1.4.6",
    "cookie-session": "^2.0.0",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "joi": "^17.9.1",
    "joi-objectid": "^4.0.2",
    "jsonwebtoken": "^9.0.0",
    "mongodb": "3.6",
    "mongoose": "^7.0.3",
    "multer": "^1.4.5-lts.1",
    "node-cron": "^3.0.2"
  }
}

This is my tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "strict": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist"
  },
  "include": ["app.ts"],
  "exclude": ["node_modules"]
}
英文:

i am trying to deploy my TS+Node.js to render.com and it shows :

`Jun 26 01:51:02 PM ==> Starting service with 'node app.ts' Jun 26 01:51:02 PM internal/process/esm_loader.js:74 Jun 26 01:51:02 PM internalBinding('errors').triggerUncaughtException( Jun 26 01:51:02 PM ^ Jun 26 01:51:02 PM Jun 26 01:51:02 PM TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /opt/render/project/src/app.ts Jun 26 01:51:02 PM at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15) Jun 26 01:51:02 PM at Loader.getFormat (internal/modules/esm/loader.js:104:42) Jun 26 01:51:02 PM at Loader.getModuleJob (internal/modules/esm/loader.js:242:31) Jun 26 01:51:02 PM at async Loader.import (internal/modules/esm/loader.js:176:17) Jun 26 01:51:02 PM at async Object.loadESM (internal/process/esm_loader.js:68:5) { Jun 26 01:51:02 PM code: 'ERR_UNKNOWN_FILE_EXTENSION' Jun 26 01:51:02 PM }

this is my package json

{   "name": "server",   "version": "1.0.0",   "description": "",   "main": "app.ts",   "scripts": {     "start": "ts-node-dev app.ts",     "test": "echo \"Error: no test specified\" && exit 1"   },   "keywords": [],   "author": "",   "license": "ISC",   "devDependencies": {     "@types/cookie-parser": "^1.4.3",     "@types/cookie-session": "^2.0.44",     "@types/cors": "^2.8.13",     "@types/express": "^4.17.17",     "@types/jsonwebtoken": "^9.0.1",     "@types/node-cron": "^3.0.7",     "express": "^4.18.2",     "nodemon": "^2.0.22",     "ts-node": "^10.9.1",     "ts-node-dev": "^2.0.0",     "typescript": "^5.0.3"   },   "dependencies": {     "@sendgrid/mail": "^7.7.0",     "@types/joi": "^17.2.3",     "axios": "^1.4.0",     "bcrypt": "^5.1.0",     "cloudinary": "^1.37.0",     "cookie-parser": "^1.4.6",     "cookie-session": "^2.0.0",     "cors": "^2.8.5",     "dotenv": "^16.0.3",     "joi": "^17.9.1",     "joi-objectid": "^4.0.2",     "jsonwebtoken": "^9.0.0",     "mongodb": "3.6",     "mongoose": "^7.0.3",     "multer": "^1.4.5-lts.1",     "node-cron": "^3.0.2"   } }`

this is my ts.config :

{   "compilerOptions": {    "module": "commonjs",    "esModuleInterop": true,    "target": "es6",    "strict": true,    "moduleResolution": "node",    "sourceMap": true,    "outDir": "dist"  },   "include": ["app.ts"],   "exclude": ["node_modules"] }

答案1

得分: 1

Your TypeScript code needs to be compiled into JavaScript. You can either compile it on our local device or let Render do this for you.

Update the key scripts in your package.json

"scripts": {
	"start": "ts-node-dev app.ts",
	"build": "tsc"
}

In the Render Service Settings, add a Build Command...

npm install && npm run build

... and add a Start Command (in your case, it's 'dist' as declared in your 'tsconfig')

node dist/server.js

❗ Also, make sure you don’t set the Environment Variable NODE_ENV to 'production', or else npm i won’t install TypeScript, and tsc will fail.

英文:

Your TypeScript code needs to be compiled into JavaScript. You can either compile it on our local device or let Render do this for you.

Update the key scripts in your package.json

"scripts": {
	"start": "ts-node-dev app.ts",
	"build": "tsc"
}

In the Render Service Settings add a Build Command...

npm install && npm run build

... and add a Start Command (in your case it's 'dist' as declared in your 'tsconfig')

node dist/server.js

❗ Also, make sure you don’t set the Environment Variable NODE_ENV to 'production', or else npm i won’t install typescript, and tsc will fail.

huangapple
  • 本文由 发表于 2023年6月26日 20:36:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定