1

I am using NestJS and TypeORM and want to connect to my postgres database. But I am getting all kinds of errors. I am pretty sure it has to do with the ormconfig.ts and the entities in there. This is the file right now:

require('dotenv').config();

module.exports = {
    host: process.env.DB_HOST,
    type: 'postgres',
    port: process.env.DB_PORT,
    username: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: process.env.DB_NAME,
    synchronize: true,
    entities: [__dirname + '/../**/*.entity.{js,ts}'],
};

and I am getting SyntaxError: Cannot use import statement outside a module Error in my Entity File that looks like this:

import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Software extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    name: string;

    @Column()
    description: string; 
}

Fixes that I have tried:

  • Adding
{
  // ...
  "type": "module",
  // ...
}

to my package.json from this question "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6 (then I get Object.defineProperty(exports, "__esModule", { value: true }); ReferenceError: exports is not defined)

  • changing entities: [__dirname + '/../**/*.entity.{js,ts}'] in the ormconfig.ts to autoLoadEntities: true,, but then I get EntityMetadataNotFound: No metadata for "Software" was found.

  • not using the path, but instead do entities: [Software] in ormconfig.ts, but it doesnt like the import statement SyntaxError: Cannot use import statement outside a module in the ormconfig.ts

It seems like everytime I fix one error, there is another one right away. Does anyone have any ideas as to why it might not work?

Lachr
  • 63
  • 8
  • Does this answer your question? [TypeORM Entity in NESTJS - Cannot use import statement outside a module](https://stackoverflow.com/questions/59435293/typeorm-entity-in-nestjs-cannot-use-import-statement-outside-a-module) – Jay McDoniel Mar 17 '21 at 20:46
  • No I tried nearly every answer. It wont let me do `entities: [join(__dirname, '**', '*.entity.{ts,js}')]` because it will give me `SyntaxError: Cannot use import statement outside a module` for the join import. I tried adding `"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js"` to the package.json, nothing seems to work sadly – Lachr Mar 18 '21 at 08:39
  • The reason for the error is that the command is finding `ts` files when `node` needs `js` files to run. The information in the linked issue is valid and functional. – Jay McDoniel Mar 18 '21 at 14:54

0 Answers0