How to resolve missing file error in Knex seed file?

40 views Asked by At

Using Knex and Typescript I have a project and I previously had a massive seed.ts file that was working as required. The file was over 800 lines so in an effort to simplify everything, I split up the seed files into all of the different tables. The tables that I want to populate with records include Categories, Products and Offerings. With everything split up, this is the current seed.ts file:

import { Knex } from 'knex';
import { seedCategories } from './seedCategories';
import { seedProducts } from './seedProducts';
import { seedOfferings } from './seedOfferings';

export async function seed(knex: Knex): Promise<void> {
  await seedCategories(knex);
  await seedProducts(knex);
  await seedOfferings(knex);
}
 

All of these files exist and in my package.json I have the scripts below that run the project and therefore the seeds as required:

"scripts": {
    "start": "npm run knex:latest && node --experimental-specifier-resolution=node dist/index.js",
    "dev": "npm run knex:latest && npm run knex:seed && npm run dev:watch",
    "build": "rimraf dist && swc --config-file ./.swcrc ./src -d dist",
    "test:script": "node --experimental-specifier-resolution=node --test --experimental-test-coverage test",
    "test": "npm run knex:latest && npm run test:compile && npm run test:script ",
    "knex:run": "node --loader ./node_modules/ts-node/esm/transpile-only.mjs ./node_modules/knex/bin/cli.js --knexfile ./src/db/knexfile.ts",
    "knex:migrate": "npm run knex:run migrate:make $NAME -x ts",
    "knex:latest": "npm run knex:run migrate:latest",
    "knex:seed": "npm run knex:run seed:run",
  },

The overall project has a makefile. When I run it locally, I call make local-up that is defined as:

local-up:
    docker-compose -f docker-compose.local.yml up --build --force-recreate --renew-anon-volumes

After splitting up the seed into multiple files as described above, when I run make local-up I get the following error indicating that the imports cannot be recognized but I'm not entirely sure why.

> [email protected] knex:run
> node --loader ./node_modules/ts-node/esm/transpile-only.mjs ./node_modules/knex/bin/cli.js --knexfile ./src/db/knexfile.ts seed:run
 
 (node:86) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
 (Use `node --trace-warnings ...` to show where the warning was created)
 Working directory changed to /app/src/db
 Using environment: development
 Cannot find module '/app/src/db/seeds/seedCategories' imported from /app/src/db/seeds/seed.ts
 Error: Cannot find module '/app/src/db/seeds/seedCategories' imported from /app/src/db/seeds/seed.ts
     at finalizeResolution (/app/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366:11)
     at moduleResolve (/app/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:801:10)
     at Object.defaultResolve (/app/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:912:11)
     at /app/node_modules/ts-node/src/esm.ts:218:35
     at entrypointFallback (/app/node_modules/ts-node/src/esm.ts:168:34)
     at /app/node_modules/ts-node/src/esm.ts:217:14
     at addShortCircuitFlag (/app/node_modules/ts-node/src/esm.ts:409:21)
     at resolve (/app/node_modules/ts-node/src/esm.ts:197:12)

For reference, I have included my Knexfile. Note, that I only want the seeds to run in the development environment. They have therefore been omitted from the other environments.

import { dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
// Update with your config settings.

interface KnexConfig {
  [key: string]: object;
}

export const config: KnexConfig = {
  testing: {
    client: 'postgresql',
    connection: {
      port: process.env.DATABASE_PORT,
      host: process.env.DATABASE_HOST,
      database: process.env.DATABASE_NAME,
      user: process.env.DATABASE_USER,
      password: process.env.DATABASE_PASSWORD,
    },
    pool: {
      min: 2,
      max: 10,
    },
    migrations: {
      tableName: 'knex_migrations',
      directory: `${__dirname}/migrations`,
      loadExtensions: ['.ts', '.js'],
    },
    seeds: {
      directory: `${__dirname}/seeds`,
      loadExtensions: ['.ts', '.js'],
    },
  },
  development: {
    client: 'postgresql',
    connection: {
      port: process.env.DATABASE_PORT,
      host: process.env.DATABASE_HOST,
      database: process.env.DATABASE_NAME,
      user: process.env.DATABASE_USER,
      password: process.env.DATABASE_PASSWORD,
    },
    pool: {
      min: 2,
      max: 10,
    },
    migrations: {
      tableName: 'knex_migrations',
      directory: `${__dirname}/migrations`,
      loadExtensions: ['.ts', '.js'],
    },
    seeds: {
      directory: `${__dirname}/seeds`,
      loadExtensions: ['.ts', '.js'],
    },
  },

  production: {
    client: 'postgresql',
    connection: {
      port: process.env.DATABASE_PORT,
      host: process.env.DATABASE_HOST,
      database: process.env.DATABASE_NAME,
      user: process.env.DATABASE_USER,
      password: process.env.DATABASE_PASSWORD,
    },
    pool: {
      min: 2,
      max: 10,
    },
    migrations: {
      tableName: 'knex_migrations',
      directory: `${__dirname}/migrations`,
    },
  },
};

export default config;

Any assistance to resolve this issue would be much appreciated!

0

There are 0 answers