Andreas
Andreas

Reputation: 1776

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?

Upvotes: 9

Views: 15786

Answers (5)

Liakos
Liakos

Reputation: 572

How to use Knex CLI

Unix Shell

Locally

npm i knex
NODE_ENV=development npx knex migrate:list
# or
export NODE_ENV=development && npx knex migrate:list

Globally

npm i knex -g
NODE_ENV=development knex migrate:list
# or
export NODE_ENV=development && knex migrate:list

Windows Shell

Locally

npm i knex
set NODE_ENV=development&& npx knex migrate:list

Globally

npm i knex -g
set NODE_ENV=development&& knex migrate:list

Consider using Git Bash Shell which behaves like a Unix shell in addition to including the Windows environment.

Upvotes: 0

zmonteiro
zmonteiro

Reputation: 371

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

Upvotes: 3

diogo.silva
diogo.silva

Reputation: 448

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.

Upvotes: 3

chigozie okolie
chigozie okolie

Reputation: 21

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.

Upvotes: 2

Mikael Lepistö
Mikael Lepistö

Reputation: 19718

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.

Upvotes: 11

Related Questions