Skip to content
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

Add 'cjs' to list of Seeder.js extensions to support Node "type": "Module" projects #4381

Open
adamlove01 opened this issue Mar 19, 2021 · 3 comments

Comments

@adamlove01
Copy link
Contributor

Environment

Knex version: 0.21.17
Database + version: postgres (PostgreSQL) 11.5
OS: MacOS Big Sur 11.2.3
NodeJS: v14.15.1

Bug

I am running NodeJS with ES modules enabled ("type": "module") and with Knex running with .cjs extension:

knexfile.cjs
knex.cjs

Migration files and seed file also have .cjs extension:
migrations
↳ articleTable.cjs
↳ userTable.cjs
seeds
↳ articleTable.cjs

I can run migrations like this:
npx knex migrate:latest --knexfile knexfile.cjs

This works normally.

Error

But when I run the seed like this:
npx knex seed:run --knexfile knexfile.cjs

I receive error "No seed files exist"

Solution

I discovered the problem, which is in the Seeder.js file's setConfig(config) {} function:

  setConfig(config) {
    return extend(
      {
        extension: 'js',
        directory: './seeds',
        loadExtensions: [
          '.co',
          '.coffee',
          '.eg',
          '.iced',
          '.js',
          '.litcoffee',
          '.ls',
          '.ts',
        ],
        timestampFilenamePrefix: false,
        sortDirsSeparately: false,
        recursive: false,
      },
      this.config || {},
      config
    );
  }

If I add '.cjs' to the loadExtensions array, my seed file runs normally:

  setConfig(config) {
    return extend(
      {
        extension: 'js',
        directory: './seeds',
        loadExtensions: [
          '.co',
          '.coffee',
          '.eg',
          '.iced',
          '.js',
          '.litcoffee',
          '.ls',
          '.ts',
          '.cjs',
        ],
        timestampFilenamePrefix: false,
        sortDirsSeparately: false,
        recursive: false,
      },
      this.config || {},
      config
    );
  }

Proposal

I propose adding '.cjs' to the list of extensions to support using Knex in Node projects with "type": "module" enabled while running Knex in CommonJS mode.

@kibertoad
Copy link
Collaborator

@adamlove01 Could you please send a PR for this?

@adamlove01
Copy link
Contributor Author

Sure!

@adamlove01
Copy link
Contributor Author

#4382

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants