Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Knex CLI

I have installed Knex in my Node project and all is wonderful and great... so far...

Now I dig deeper into Knex and am confronted with migrations. All the docs I found talk about running commands like "knex migrate:latest", etc. What I get as a result when I try to run such commands from the (Windows) command line is an error telling me that 'knex' is an unknown command.

I am not a npm and Nodes expert, just enough to get the basics running. When digging into the knex node package I find some configuration for a cli.js file under a 'bin' section in the 'package.json'. I do not understand these configurations, even reading the npm documentation about this 'bin' section does not make it clearer to me.

So here my question: I am on Windows 10 and have installed a package like 'knex' local to my project. Knex comes with a cli. What do I need to do to call that cli from my console?

like image 456
Andreas Avatar asked May 23 '18 11:05

Andreas


4 Answers

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.

like image 161
Mikael Lepistö Avatar answered Oct 10 '22 19:10

Mikael Lepistö


In your console, try to $ npx knex migrate:latest It helped me

like image 43
zmonteiro Avatar answered Oct 06 '22 00:10

zmonteiro


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.

like image 2
chigozie okolie Avatar answered Oct 10 '22 20:10

chigozie okolie


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.

like image 2
diogo.silva Avatar answered Oct 10 '22 19:10

diogo.silva