How to use the Knex CLI
First type in "npx knex" to access options and commands available to the knex module. To be able to make use of the Knex cli that comes bundled with it, you then have to access the knex module from whatever path you intend creating the file from. For example, let's say I was in the migrations directory and the node_modules folder is one path higher, I would access the Knex module in it this way '../node_modules/.bin/knex migrate:make create-user-table.js' to be able to create a 'create-user-table.js', migration file. I hope I'm clear enough.
You can find client from node_modules/.bin/knex
if you haven't installed knex globally (which I don't recommend).
When you install packages locally to some directory, all the "bin" executables are linked automatically under node_modules/.bin
. If you use these scripts from package.json scripts, npm automatically adds node_modules/.bin
to path, so inside package json you don't have to refer node_modules/.bin/knex
but just knex
is enough.
If you have knex installed in your project, you can make it available for use via your shell by adding a simple script to your package.json
.
"scripts": {
"knex": "knex"
}
Now you can use the knex cli with npm run knex
or, if you use yarn, yarn knex
.