FE - Eggjs 结合 Typeorm 出现连接不了数据库

问题

eggjs 结合 typeorm 打包部署的时候,一致报错,刚开始还以为是 eggjs 的问题

[egg-scripts] Got error when startup: 
[egg-scripts]     at createScript (vm.js:251:10)
[egg-scripts]     at Object.runInThisContext (vm.js:303:10)
[egg-scripts]     at Module._compile (internal/modules/cjs/loader.js:656:28)
[egg-scripts]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
[egg-scripts]     at Module.load (internal/modules/cjs/loader.js:598:32)
[egg-scripts]     at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
[egg-scripts]     at Function.Module._load (internal/modules/cjs/loader.js:529:3)
[egg-scripts]     at Module.require (internal/modules/cjs/loader.js:636:17)
[egg-scripts]     at require (internal/modules/cjs/helpers.js:20:18)
[egg-scripts] 
[egg-scripts] pid: 30800
[egg-scripts] hostname: labelnet
[egg-scripts] 
[egg-scripts] 2019-03-05 15:17:28,085 ERROR 30796 [-/127.0.0.1/-/1ms GET /] nodejs.SyntaxError: Unexpected token {
[egg-scripts] (function (exports, require, module, __filename, __dirname) { import {
[egg-scripts]                                                                      ^
[egg-scripts] 
[egg-scripts] SyntaxError: Unexpected token {
[egg-scripts]     at new Script (vm.js:79:7)
[egg-scripts]     at createScript (vm.js:251:10)
[egg-scripts]     at Object.runInThisContext (vm.js:303:10)
[egg-scripts]     at Module._compile (internal/modules/cjs/loader.js:656:28)
[egg-scripts]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
[egg-scripts]     at Module.load (internal/modules/cjs/loader.js:598:32)
[egg-scripts]     at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
[egg-scripts]     at Function.Module._load (internal/modules/cjs/loader.js:529:3)
[egg-scripts]     at Module.require (internal/modules/cjs/loader.js:636:17)
[egg-scripts]     at require (internal/modules/cjs/helpers.js:20:18)

eggjs 的 issues 都看过类似的问题了 ,

https://github.com/eggjs/egg/issues/2388

但官网文档已经明确说明,先回加载 js 文件 后加载 ts 文件,一直以为是 eggjs 的问题;

运行 npm start 不会加载 ts

没有想到是 typeorm 的配置问题,entity 配置的是 ts 文件,生产环境需要配置 js 文件

typeorm 并不会自动加载 entity 的 js 文件

解决

开发环境 entity 加载 ts 文件 ,生产环境 加载 js 文件

   "db_dev": {
        "database": "ruisite-dash-server",
        "host": "127.0.0.1",
        "port": "3306",
        "username": "root",
        "password": "root",
        "logging": true,
        "entities": ["app/entity/*.ts", "app/entity/*/*.ts"]
    }
    
    "db_production": {
        "database": "ruisite-dash-server",
        "host": "192.168.100.10",
        "port": "3306",
        "username": "root",
        "password": "root",
        "logging": true,
        "entities": ["app/entity/*.js", "app/entity/*/*.js"]
    }

你可能感兴趣的:(web,前端)