Search code examples
node.jspostgresqltypescriptmacostypeorm

typeorm createConnection return Pending even with Await on MacOS with PG


MacOS Catalina 10.15.4

How to reproduce, simply install using quick guide

npm install typeorm --save
npm install reflect-metadata --save
npm install @types/node --save
npm install pg --save
npm install typeorm -g
typeorm init --name MyProject --database postgres
cd MyProject
npm install
npm start

It will simply return

➜  MyProject npm start

> [email protected] start /Users/sergecolle/work/eve/MyProject
> ts-node src/index.ts

When troubleshooting I found that neither the code in then and catch got executed. After modifying the code, to something like

const connection:Connection = await createConnection()

the connection will be in state Pending and then the program simply skip then and catch and terminate.

I have ask a friend to try it on his mac and he get the same issue.

there the package.json generated by the typeorm install

   "name": "MyProject",
   "version": "0.0.1",
   "description": "Awesome project developed with TypeORM.",
   "devDependencies": {
      "ts-node": "3.3.0",
      "@types/node": "^8.0.29",
      "typescript": "3.3.3333"
   },
   "dependencies": {
      "typeorm": "0.2.25",
      "reflect-metadata": "^0.1.10",
      "pg": "^7.3.0",
      "express": "^4.15.4",
      "body-parser": "^1.18.1"
   },
   "scripts": {
      "start": "ts-node src/index.ts"
   }

and the ormconfig.json

{
   "type": "postgres",
   "host": "localhost",
   "port": 5432,
   "username": "test",
   "password": "test",
   "database": "test",
   "synchronize": true,
   "logging": false,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }
}

and made sure a postgres db with those credential existed using the psql command.

The tsconfig.json generated

{
   "compilerOptions": {
      "lib": [
         "es5",
         "es6"
      ],
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "outDir": "./build",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "sourceMap": true
   }
}

The lib and target seem a little old to support async/await. I tried updating it to es2017, es2018, and esnext but no luck


Solution

  • It appears to be a problem with pg (node-postgres). As far as I can see, you're running version 7.3.0. Try uninstalling pg with npm uninstall pg --save and then install it again with npm install pg --save . This will install the latest version (which for me was 8.2.1 )

    Additional info: I had a project working fine on Ubuntu and then set it up on my MacBook, where I experienced the same issue. I also tried using ConnectionManager with different connection options and it still didn't work. The only thing that worked was the solution I mentioned above