Knex: migration:make使用TypeScript失败

创建于 2019-01-18  ·  27评论  ·  资料来源: knex/knex

环境

Knex版本:0.16.3(使用TypeScript knexfile)
数据库+版本:PostgreSQL 10.5
操作系统:Windows 10 Pro 64 Bit

虫子

运行knex migrate:make somename ,输出为:

Requiring external module ts-node/register
Using environment: development
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
    at assertPath (path.js:39:11)
    at Object.resolve (path.js:166:7)
    at directories.map.directory (X:\Code\node\knextest\node_modules\knex\lib\migrate\MigrationGenerator.js:82:28)
    at Array.map (<anonymous>)
    at MigrationGenerator._absoluteConfigDirs (X:\Code\node\knextest\node_modules\knex\lib\migrate\MigrationGenerator.js:81:24)
    at MigrationGenerator._ensureFolder (X:\Code\node\knextest\node_modules\knex\lib\migrate\MigrationGenerator.js:41:23)
    at MigrationGenerator.make (X:\Code\node\knextest\node_modules\knex\lib\migrate\MigrationGenerator.js:35:17)
    at Migrator.make (X:\Code\node\knextest\node_modules\knex\lib\migrate\Migrator.js:122:27)
    at Command.commander.command.description.option.action (X:\Code\node\knextest\node_modules\knex\bin\cli.js:179:10)
    at Command.listener (X:\Code\node\knextest\node_modules\commander\index.js:315:8)
    at Command.emit (events.js:182:13)
    at Command.parseArgs (X:\Code\node\knextest\node_modules\commander\index.js:654:12)
    at Command.parse (X:\Code\node\knextest\node_modules\commander\index.js:474:21)
    at Liftoff.invoke (X:\Code\node\knextest\node_modules\knex\bin\cli.js:278:13)
    at Liftoff.execute (X:\Code\node\knextest\node_modules\liftoff\index.js:203:12)
    at module.exports (X:\Code\node\knextest\node_modules\flagged-respawn\index.js:51:3)

这是我的knexfile.ts

module.exports = {
  development: {
    client: 'pg',
    connection: {
      host: 'localhost',
      database: 'postgres',
      user: 'postgres',
      password: 'pw',
    },
    migrations: {
      directory: 'migrations',
    },
  },
};

其他一些细节:

  • knex migrate:make somename作品如果我改变knexfile扩展.js 。 (knex 0.16.3)
  • 在使用knex 0.15.1和@types/knex 0.15.1软件包时, knex migrate:make somename可以与TypeScript knexfile一起使用。
  • 我没有尝试过其他数据库引擎或knex 0.15.1-0.16.3之间的版本。
bug migrations

最有用的评论

我们还使用Knex将查询写入PostgreSQL服务器。 我们发现最好的生成方法如下。

  1. 生成迁移文件
knex migrate:make --knexfile knexfile.ts -x ts <your-migration-name>
  1. 运行迁移文件
knex migrate:latest --knexfile knexfile.ts 
  1. 生成种子文件
knex seed:make --knexfile knexfile.ts -x ts <your-seed-name>
  1. 运行种子文件
knex seed:run --knexfile knexfile.ts 

--knexfile knexfile.ts如果您使用不会是必要的knexfile.js因为打字稿不能提供任何类型的检查您的配置文件。

所有27条评论

我认为这可能与以下内容有关: https :
我可以确认它可以与0.15.2 ,但不能与0.16.x

我认为这可能与以下问题有关:#2998
我可以确认它适用于0.15.2,但不适用于0.16.x

不,这是另一个问题。 并且我可以确认#2998无法使用给定的信息进行复制。

听起来像自动knexfile解析不会查找knexfile.ts,您可以通过以下方式将--knexfile knexfile.ts参数传递给command或直接给迁移生成器命令指定路径:

knex migrate:make --migrations-directory . -x ts migration-name

我想客户端应该自动寻找knexfile.ts。

我首先用migrate:latest注意到了这一点,有趣的是,它引发了与migrate:make不同的错误。 这是一个简单的复制品,您可以在其中看到它们: https :

今天还注意到, knex --knexfile knexfile.ts migrate:make (...)将创建一个.js文件而不是.ts文件。 (以前,当knex自动获取knexfile.ts ,也会自动产生.ts迁移)

我们还使用Knex将查询写入PostgreSQL服务器。 我们发现最好的生成方法如下。

  1. 生成迁移文件
knex migrate:make --knexfile knexfile.ts -x ts <your-migration-name>
  1. 运行迁移文件
knex migrate:latest --knexfile knexfile.ts 
  1. 生成种子文件
knex seed:make --knexfile knexfile.ts -x ts <your-seed-name>
  1. 运行种子文件
knex seed:run --knexfile knexfile.ts 

--knexfile knexfile.ts如果您使用不会是必要的knexfile.js因为打字稿不能提供任何类型的检查您的配置文件。

有任何解决方案吗? 这个问题很难增加Knex的采用率:/

@dgadelha感谢您的提醒! 我今天或明天看看。

修复此问题的

今天还注意到, knex --knexfile knexfile.ts migrate:make (...)将创建一个.js文件而不是.ts文件。 (以前,当knex自动获取knexfile.ts ,也会自动产生.ts迁移)

@kibertoad刚刚安装了0.17.6,即使在Knexfile中指定“ .ts”,该行为仍然会发生。 它们是单独的错误吗?

@mathieumg是的。 请参阅我在几分钟前创建的PR,并从此问题中引用它:D

@kibertoad我做到了! 该说明听起来像是基于与Knexfile相同的扩展名来推断扩展名,所以我不确定。

The description makes it sounds like that was rather for inferring the extension based on the same extension as the Knexfile ->好,是的,它是从knexfile自动推断扩展名,无论是明确指定扩展名还是自动解析的扩展名。
TS knexfile的0.17.6固定自动分辨率。

好的! 因为带有extension: "ts" 0.17.6仍然给出“ .js”结果。 感谢您修复这些错误! 😄

因为带有扩展名:“ ts”的0.17.6仍然给出了“ .js”的结果

啊,你的意思是你有knexfile

{
...
migrations: {
  extension: "ts"
}

在里面? 该错误已在https://github.com/tgriesser/knex/commit/a65a95bc672563e01c08b40384e1dc2b079ecee1中修复,您可以通过在此提交中锁定版本来访问它(如果您不想直接指向master)。
不幸的是,它不会被反向移植到0.17

0.18.0仍然遥遥领先,但是如果您不依赖提交,我可以发布next-1。

嘿,我评论了PR,非常感谢!

@kibertoad太棒了,谢谢您的精确! 我们可以等待0.18,没有压力。 😄

@kibertoad您引用的提交仅在直接在下面定义时才有效

module.exports = {
  migrations: {
    extension: 'ts';
  }
}

但是,必须在以下位置定义定义放置和读取迁移的位置的迁移对象

module.exports = {
  [environment]: {
    migrations: {
      directory: './migrations';
    }
}

如果将migrations对象移出环境对象,则迁移目录将被忽略。

@ViggoV感谢您的错误报告! 今天将尝试解决此问题。

@ViggoV您现在可以在您的情况下正常使用最新的master并进行报告吗?

在0.18.0-next2中发布

抱歉,我最终切换到{ ext: 'ts' } ,该代码可以正常工作,但似乎没有记录在案。 有时间我会测试

嗨,我有同样的问题。 我解决了这样在knexfile.ts中要求ts-node / register的问题

`
要求('ts-node / register')
const s = require(“ ./ settings.js”);
module.exports = {

client: 'pg',
connection: {
  user: s.user,
  host: s.host,
  database: 'starwars',
  password: s.password,
  ssl: true

}
};

`

我不知道您是否已经解决了问题,但就我而言,解决方案非常简单。 我只是将_knexfile.js_移动到项目的根目录。 我将knex安装在目录“ / backend”中,但由于未知原因,文件已移至其他目录。 因此,我将knexfile.js移到了“ backend / knexfile.js”,并且像一个

@moacirandretti ,我的knexfile.ts它在根项目中,但是cli heroku heroku run knex --knexfile knexfile.ts migrate: latest
返回此消息。

Running knex --knexfile knexfile.ts migrate: latest on ⬢ backend-ecolab ... up, run.5056 (Free)
Failed to load external module ts-node / register
Failed to load external module typescript-node / register
Failed to load external module typescript-register
Failed to load external module typescript-require
Failed to load external module sucrase / register / ts
Failed to load external module @ babel / register
Error: Cannot find module 'ts-node / register'
Require stack:
- /app/knexfile.ts
- /app/node_modules/knex/bin/cli.js
    at Function.Module._resolveFilename (internal / modules / cjs / loader.js: 966: 15)
    at Function.Module._load (internal / modules / cjs / loader.js: 842: 27)
    at Module.require (internal / modules / cjs / loader.js: 1026: 19)
    at require (internal / modules / cjs / helpers.js: 72: 18)
    at Object. <anonymous> (/app/knexfile.ts:1:1)
    at Module._compile (internal / modules / cjs / loader.js: 1138: 30)
    at Object.Module._extensions..js (internal / modules / cjs / loader.js: 1158: 10)
    at Module.load (internal / modules / cjs / loader.js: 986: 32)
    at Function.Module._load (internal / modules / cjs / loader.js: 879: 14)
    at Module.require (internal / modules / cjs / loader.js: 1026: 19)

我在dev依赖中安装了ts-node

我遇到了同样的错误。
knexfile.ts:

import dotenv from "dotenv";
import { Config } from "knex";
dotenv.config();

interface KnexConfig {
  [name: string]: Config;
}

const knexConfig: KnexConfig = {
  development: {
    client: "pg",
    connection: {
      host: process.env.DB_HOST || "127.0.0.1",
      user: process.env.DB_USERNAME || "postgres",
      password: process.env.DB_PASSWRORD || "postgres",
      database: process.env.DB_DEV_NAME || "done_list",
    },
    migrations: {
      directory: "./migrations",
      extension: "ts",
    },
  },
  test: {
    client: "pg",
    connection: {
      host: process.env.DB_HOST || "127.0.0.1",
      user: process.env.DB_USERNAME || "postgres",
      password: process.env.DB_PASSWRORD || "postgres",
      database: process.env.DB_DEV_NAME || "done_list_test",
    },
  },
};

export { knexConfig };

当我运行knex migrate:make user时出现错误,但是当我运行knex migrate:make --migrations-directory ./scr/migrations -x ts user本注释所述

打字稿: 4.0.2
结节: 0.21.5
@ types / knex: 0.16.1

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

npow picture npow  ·  3评论

mattgrande picture mattgrande  ·  3评论

arconus picture arconus  ·  3评论

mishitpatel picture mishitpatel  ·  3评论

mtom55 picture mtom55  ·  3评论