【发布时间】:2021-08-19 06:06:29
【问题描述】:
当我运行 yarn knex migrate:make init 时,我收到此错误
Failed to resolve config file, knex cannot determine where to generate migrations
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
at validateString (internal/validators.js:120:11)
at Object.resolve (path.js:139:9)
at D:\NEXT-JS\pen-neuton\server\node_modules\knex\lib\migrations\migrate\MigrationGenerator.js:77:19
at Array.map (<anonymous>)
at MigrationGenerator._absoluteConfigDirs (D:\NEXT-JS\pen-neuton\server\node_modules\knex\lib\migrations\migrate\MigrationGenerator.js:71:24)
at MigrationGenerator._ensureFolder (D:\NEXT-JS\pen-neuton\server\node_modules\knex\lib\migrations\migrate\MigrationGenerator.js:28:23)
at MigrationGenerator.make (D:\NEXT-JS\pen-neuton\server\node_modules\knex\lib\migrations\migrate\MigrationGenerator.js:20:16)
at Migrator.make (D:\NEXT-JS\pen-neuton\server\node_modules\knex\lib\migrations\migrate\Migrator.js:320:27)
at Command.<anonymous> (D:\NEXT-JS\pen-neuton\server\node_modules\knex\bin\cli.js:218:10)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
error Command failed with exit code 1.
knexfile.js
require("dotenv").config();
module.exports = {
development: {
client: "pg",
useNullAsDefault: true,
debug: process.env.NODE_ENV === 'development',
connection: {
host: process.env.POSTGRES_DEV_HOST,
port: process.env.POSTGRES_DEV_PORT,
user: process.env.POSTGRES_DEV_USER,
password: process.env.POSTGRES_DEV_PASSWORD,
database: process.env.POSTGRES_DEV_DATABASE
},
migrations: {
directory: "./migrations"
},
seeds: {
directory: "./seeds"
}
}
};
数据库/index.js
const knexfile = require('./knexfile')
const debug = require('debug')
const knex = require('knex')
const util = require('util')
const log = debug('app:db')
const env = process.env.NODE_ENV
const config = knexfile[env]
log(`Connected to ${config.client} with parameters: ${util.inspect(config.connection, { colors: true })}`)
module.exports = knex(config)
有人建议我在全球范围内安装 Knex,运行 knex migrate:make init 并成功了。但我想知道它对生产有好处吗?
【问题讨论】:
-
knexfile.js 在哪里? knexfile.js 和 package.json 是否位于同一级别?
-
不,这两个文件位于不同的文件夹中。因为我希望它们在不同的文件夹中
-
那么,您可能需要通过
--knexfile像这样yarn knex migrate:make init --knexfile=./config/knexfile.jsyarn knex migrate:make init --knexfile=./config/knexfile.js来定义配置文件的位置 -
我尝试了类似的东西,
yarn knex migrate:make init --knexfile --directory ./config/knexfile.js。这没有用。虽然我会试试这个。 -
knex cli 选项中没有
--directory。
标签: node.js postgresql express knex.js