New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support .ts ormconfig in CLI #2828

ssalka opened this Issue Sep 20, 2018 · 6 comments


None yet
5 participants
Copy link

ssalka commented Sep 20, 2018

Issue type:

[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

I have an existing configuration file written in TypeScript, currently being used successfully at runtime. The config (config/mongodb.ts) is as follows:

export default {
  host: 'localhost',
  port: 27017,
  database: 'test',
  type: 'mongodb'

However, I'm unable to use this config file via the CLI - I get the following from running typeorm schema:sync --config ./config/mongodb:

(function (exports, require, module, __filename, __dirname) { export default {

SyntaxError: Unexpected token export

I was a little surprised by this. Given that the project is written in TypeScript, and is focused on providing functionality around TypeScript, it seems odd that developers end up having to use a .js or .json file to specify their config.

I've tried using a JSON file instead, which works for the CLI, but causes the following type error:

TSError: ⨯ Unable to compile TypeScript:
config/index.ts(6,14): error TS2322: Type '{ "host": string; "port": string; "database": string; "type": string; }' is not assignable to type 'MongoConnectionOptions'.
  Types of property 'type' are incompatible.
    Type 'string' is not assignable to type '"mongodb"'.

I could patch this by adding an as any type assertion, or by transpiling the file for usage with the CLI, but I'm generally against doing either - I'd prefer to use a TypeScript config file as the source of truth.


This comment has been minimized.

Copy link

ryuuji3 commented Oct 9, 2018

I would recommend using ts-node to run the CLI; try pre-facing typeorm commands with:

ts-node ./node_modules/.bin/typeorm

In our project, I have npm commands that use the CLI like this to make things easier.
Let me know if this works since I would like to try using .ts files instead of .js myself. Just hadn't thought of trying it til now.


This comment has been minimized.

Copy link

ssalka commented Oct 11, 2018

@ryuuji3 thanks for your suggestion - although this works as far as getting typeorm to read the config file I specify, I now end up with a MissingDriverError. This seems to be because the TS config file is parsed as { default: {/* actual config here */} } rather than the actual object being exported (this is expected TypeScript behavior). The result is that the reader interprets the driver field as undefined.

In the end, I can get the schema sync to run successfully by exporting each key individually, i.e.

export const type = 'mongodb';
export const host = 'localhost';

But that is also non-ideal because 1) it is less readable than a single object or JSON file, and 2) I lose the ability to enforce the MongoConnectionOptions interface.


This comment has been minimized.

Copy link

ryuuji3 commented Oct 11, 2018


This comment has been minimized.

Copy link

ssalka commented Oct 11, 2018

Thanks @ryuuji3, I believe that will do as a workaround (ideally, would prefer that ConnectionOptionsReader can extract a default export from a .ts file).

I'm using TypeScript, so the module variable cannot be used as such. There is a TS-equivalent syntax though (which I've never used before) that appears to work:

export = {/* config */};

Then in my application code, I can write either import * as ormconfig from '...' or import ormconfig = require('...')


This comment has been minimized.

Copy link

zefexdeveloper commented Nov 9, 2018

I'm getting this error after running ts-node ./node_modules/.bin/typeorm

basedir=$(dirname "$(echo "$0" | sed -e 's,\,/,g')")
SyntaxError: missing ) after argument list


This comment has been minimized.

Copy link

hackmushroom commented Nov 11, 2018

@zefexdeveloper I had the same issue. I now use a script entry like this in my package.json to get it working:

"scripts": {
    "typeorm:cli": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -f ./ormconfig"

That way I can run commands easily. E.g.:

yarn typeorm:cli migration:run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment