How to use the Knex CLI

2019-06-28 05:53发布

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?

2条回答
Evening l夕情丶
2楼-- · 2019-06-28 06:00

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.

查看更多
欢心
3楼-- · 2019-06-28 06:02

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.

查看更多
登录 后发表回答