Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
createConnection
✓ import { createConnection } from 'typeorm'
✗ const createConnection = require('typeorm').createConnection
Both ESM and CJS work, but ESM is preferred in TypeScript projects.
Entity
✓ import { Entity } from 'typeorm'
✗ const Entity = require('typeorm').Entity
Used to decorate classes as entities.
PrimaryGeneratedColumn
✓ import { PrimaryGeneratedColumn } from 'typeorm'
✗ import { PrimaryGeneratedColumn } from 'typeorm/decorators'
Export is from the main package, not a subpath.
BaseEntity
✓ import { BaseEntity } from 'typeorm'
✗ import { BaseEntity } from 'typeorm/entity'
BaseEntity is exported from the top-level module, not a subpath.
Demonstrates creating a SQLite in-memory database with TypeORM, defining a User entity, saving and retrieving records.
import { createConnection, Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
@Entity()
class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
}
async function main() {
const connection = await createConnection({
type: 'sqlite',
database: ':memory:',
entities: [User],
synchronize: true,
});
const user = new User();
user.name = 'Alice';
await connection.manager.save(user);
const users = await connection.manager.find(User);
console.log(users);
await connection.close();
}
main().catch(console.error);
Errors
Common errors & fixes
TypeError: Class constructor cannot be invoked without 'new'
Importing a class incorrectly when using ES modules and CommonJS mixed.
fixEnsure you use import { Entity } from 'typeorm' and not require('typeorm'). MissingDriverError: Wrong driver: "sql.js" given, supported drivers are: mysql, postgres, sqlite, mssql, oracle, sqljs.
Using driver name 'sql.js' instead of 'sqljs'.
fixChange driver type to 'sqljs' in connection options.
Cannot find module 'reflect-metadata'
reflect-metadata is not installed or not imported.
fixRun 'npm install reflect-metadata' and add import 'reflect-metadata' at the top of your entry file.
Audit
Dependencies
reflect-metadatarequiredRequired shim for decorator metadata emit
mysqloptionalMySQL/MariaDB driver (install one driver only)
pgoptionalPostgreSQL driver (install one driver only)
sqlite3optionalSQLite driver (install one driver only)
mssqloptionalMicrosoft SQL Server driver (install one driver only)
oracledboptionalOracle driver - experimental (install one driver only)
sql.jsoptionalsql.js driver - experimental (install one driver only)