The database migration encounters an issue: The module 'typeorm' cannot be located

When I run the following commands:

❯ node --version
v16.19.0

❯ yarn --version
3.5.0

I am attempting to launch this project:

https://github.com/felipebelinassi/typescript-graphql-boilerplate

However, when I execute:

yarn db:migrate

which runs the command:

ts-node typeorm migrations:generate -n

it fails with the error message displayed below:

❯ yarn db:migrate

node:internal/modules/cjs/loader:998
  throw err;
  ^

Error: Cannot find module './typeorm'
Require stack:
- /Users/test/repos/typescript-graphql-boilerplate/imaginaryUncacheableRequireResolveScript
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Function.resolve (node:internal/modules/cjs/helpers:109:19)
    at requireResolveNonCached (/Users/test/repos/typescript-graphql-boilerplate/node_modules/ts-node/dist/bin.js:549:16)
    at getProjectSearchDir (/Users/test/repos/typescript-graphql-boilerplate/node_modules/ts-node/dist/bin.js:519:40)
    at phase3 (/Users/test/repos/typescript-graphql-boilerplate/node_modules/ts-node/dist/bin.js:267:27)
    at bootstrap (/Users/test/repos/typescript-graphql-boilerplate/node_modules/ts-node/dist/bin.js:47:30)
    at main (/Users/test/repos/typescript-graphql-boilerplate/node_modules/ts-node/dist/bin.js:33:12)
    at Object.<anonymous> (/Users/test/repos/typescript-graphql-boilerplate/node_modules/ts-node/dist/bin.js:579:5)
    at Module._compile (node:internal/modules/cjs/loader:1165:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1219:10) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/test/repos/typescript-graphql-boilerplate/imaginaryUncacheableRequireResolveScript'
  ]
}

The contents of package.json are as follows:

{
  "name": "typescript-graphql-boilerplate",
  "version": "1.0.0",
  "license": "ISC",
  "description": "Simple TypeScript and GraphQL project boilerplate",
  "author": "Felipe Belinassi",
  "main": "index.js",
  "scripts": {
    "prebuild": "del-cli --force ./build",
    "build": "tsc",
    "prestart": "yarn build",
    "start": "node -r dotenv/config build/app.js",
    "start:dev": "ts-node-dev --transpile-only -r dotenv/config src/app.ts",
    "test": "jest --projects ./tests/integration --runInBand",
    "db:migrate": "ts-node typeorm migrations:generate -n"
  },
  "dependencies": {
    "apollo-server-express": "^2.24.0",
    "apollo-server-testing": "^2.24.0",
    "class-validator": "^0.13.1",
    "dotenv": "^9.0.0",
    "express": "^4.17.1",
    "graphql": "^15.5.0",
    "joi": "^17.4.0",
    "pg": "^8.11.3",
    "reflect-metadata": "^0.1.13",
    "type-graphql": "^1.1.1",
    "typedi": "^0.8.0",
    "typeorm": "^0.3.16",
    "typeorm-typedi-extensions": "^0.4.1"
  },
  "devDependencies": {
    "@types/express": "^4.17.11",
    "@types/jest": "^26.0.23",
    "@types/node": "^15.0.2",
    "del-cli": "^3.0.1",
    "jest": "^26.6.3",
    "ts-jest": "^26.5.6",
    "ts-node": "^10.9.1",
    "ts-node-dev": "^2.0.0",
    "typescript": "^5.2.2"
  }
}

Any suggestions on how to resolve this issue? Postgres is running via docker-compose.

I have attempted to update dependencies but failed to populate the database with data. The issue appears to be related to TypeORM.

Answer №1

Consider utilizing DataSource instead of ormconfig as it has been deprecated. Find more information here.

Here is an example of a DataSource file:

import "reflect-metadata";
import { DataSource } from "typeorm";
import { Client } from "./entity/Client";

import { migration1661206795941 } from "./migration/1661206795941-migration";

const entities = [Client]

const migrations = [
  migration1661206795941
];

export const AppDataSource = new DataSource({
  type: "postgres",
  host: "localhost",
  port: 5434,
  username: "postgres",
  password: "postgres@2020",
  database: "postgres",
  synchronize: false,
  logging: false,
  entities: entities,
  subscribers: [],
  migrations: migrations,
});

Make sure to update your db:migrate script to:

db:migrate: typeorm migration:generate path/to/Migration -d path/to/datasource

In case you encounter the error message related to ts-node and typeorm modules:

"db:migrate": "ts-node typeorm migrations:generate -n"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing ngModel with an uninitialized object

What is the most effective way to populate an empty instance of a class with values? For example, I have a User Class and need to create a new user. In my component, I initialize an empty User Object "user: User;". The constructor sets some properties, w ...

exit out of React Dialog using a button

I have a scenario where I want to automatically open a dialog when the screen is visited, so I set the default state to true. To close the dialog, I created a custom button that, when clicked, should change the state to false. However, the dialog does no ...

When trying to access the key value of a dynamically generated object, it returns as undefined

I am facing a challenge with my student object structure... { Freshmen: [{id: 3}, {id: 5}], Sophomores: [{id: 2}, {id: 6}], Juniors: [{id: 1}, {id: 8}], Seniors: [{id: 9}, {id: 4}, {id: 7}] } My goal is to retrieve full student objects from the d ...

Utilize the legacy database for CRUD operations, while tapping into the fresh database to obtain identical information

Currently, I am faced with a challenge where I need to synchronize data between two servers using different databases. The goal is to have Server A and Server B with Database A and Database B respectively, both containing the same data, relationships, ID ...

Transforming a string such as "202309101010" into a date entity

Need to convert a string in the format "YYYYMMDDHHMM" (e.g. "202309101010") into a Date object in TypeScript? Check out this code snippet for converting the string: const dateString: string = "202309101010"; const year: number = parseInt(dateString.subst ...

Angular 2 encountering an error with the HTTP GET request

I am currently facing some challenges with subscribing to the response while using the http method get request. Below is my service implementation: import { Injectable } from '@angular/core'; import { Http, Response } from '@angular/http&ap ...

Type of event triggered by user file upload via INPUT element

I have a piece of code that reads the contents of a locally stored file. Here is what it looks like: onFile(event: any) { console.log(event); const file = event.target.files[0]; const reader = new FileReader(); reader.onloadend = (ev: any) => { ...

Dynamic Text Labels in Treemap Visualizations with Echarts

Is it possible to adjust the text size dynamically based on the size of a box in a treemap label? I haven't been able to find a way to do this in the documentation without hardcoding it. Click here for more information label: { fontSize: 16 ...

Exploring the use of Observables in Angular 2 services

Ensuring the seamless transfer of data between components is crucial in Angular development. One common way to achieve this is by utilizing observables. Let's take a look at how observables are implemented in a service: import { Injectable } from &ap ...

Is it possible to transform a ReadonlyArray<any> into a standard mutable array []?

At times, when working with Angular functions and callbacks, they may return a ReadonlyArray. However, I prefer using arrays in the traditional way and don't want to use immutable structures like those in Redux. So, what is the best way to convert a ...

Adding Components Dynamically to Angular Parent Dashboard: A Step-by-Step Guide

I have a dynamic dashboard of cards that I created using the ng generate @angular/material:material-dashboard command. The cards in the dashboard are structured like this: <div class="grid-container"> <h1 class="mat-h1">Dashboard</h1> ...

Encountering an issue with MUI Props: "Must provide 4 to 5 type arguments."

I'm attempting to use a custom component and pass in AutocompleteProps as a prop, all while utilizing typescript. Here's my current setup: type Props = { autoCompleteProps?: AutocompleteProps<T> label: string loading?: boolean } cons ...

Sending parameters with a linked Get request leads to a 404 error page

Exploring how to interact with an external API using a TS/Express server. The user will choose a product, triggering a GET request to the server, which then queries the external API for pricing data. This is a project for fun and learning purposes, so ple ...

Issue with linear Graham scan method causing failure when computing convex hull of basic polygon

It is said that the Graham scan algorithm can efficiently find the convex hull of a simple polygon in linear time without requiring the nlogn sorting step since the vertices are already effectively sorted. I have implemented the Graham scan algorithm, and ...

Best 3 selections for every category listed in a freshly created table - postgresql

I am facing a challenge with the data in Table 1. I need to find the top 3 supermarkets for each product based on price in descending order. The desired outcome is shown in Table 2 below. Despite my attempts with various SQL queries, I have not been able t ...

Issues encountered when using AngularJS2's post method to send data to an ASP.NET Core backend result

I recently delved into learning Angular2 and Asp.net core, but I encountered an issue when trying to post an object. Here is the relevant code snippet: Service.ts file: export class SubCategoryService { //private headers: Headers; constructor(private htt ...

What is the correct way to invoke a function from an external JavaScript file using TypeScript?

We are currently experimenting with incorporating Typescript and Webpack into our existing AngularJS project. While I have managed to generate the webpack bundle, we are facing an issue at runtime where the program is unable to locate certain functions in ...

My goal is to develop a secure login system with authentication on the Angular platform

login(data: any) { this.user.getUsers().subscribe( (users) => { const user = users.find((u) => u.username === data.username && u.userpassword === data.password); if (user) { // Valid username and password, ...

Having trouble locating node_modules post deployment?

Although the title may lead you astray, please stay with me for a moment. I've created a basic Angular2 application in Visual Studio 2015 and have now deployed it to Azure. While having node_modules in the development environment worked perfectly fi ...

Can the getState() method be utilized within a reducer function?

I have encountered an issue with my reducers. The login reducer is functioning properly, but when I added a logout reducer, it stopped working. export const rootReducer = combineReducers({ login: loginReducer, logout: logoutReducer }); export c ...