How to use the Knex CLI - node.js

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?

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.

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

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.

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.

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.

Related

NodeJS Sequelize ORM not recognise in Windows 10 CMD

Please i need help. I use windows 10 OS and i am trying to use the Sequelize ORM dependency package/module in my NodeJS. I have installed the sequelize dependency and the sequelize-cli dependency package using npm and I confirmed a successful installation because i can find them in my package.json file.
The problem is everytime I try to run the sequelize command to create the model and other directories so that i can start using the ORM mapping in my NodeJS file the Windows 10 cmd returns that "sequelze is not recognized as an internal or external command". I know any command to be run in CMD would be added to the environment variable but sequelize is not a software that is installed but a module inside Node that is install through npm command.
How can i make Windows 10 recognize the sequelize command?
Please i will appreciate any help whatsoever.
It's better to install sequelize-cli globally to avoid such issues. This way it will be in C:\Users\<user>\AppData\Roaming\npm (which is added in PATH) as sequelize.cmd

"npm run" commands fail on Windows

I am using Windows PowerShell ISE with administrator privileges. My project uses webpack, which is installed as a local dependency with npm. I have a "script" defined in package.json:
"build": "webpack --progress --config resources/assets/build/webpack.config.js",
When I execute npm run build I get the error message 'webpack' is not recognized as an internal or external command, but if I copy the webpack command and execute that it works fine. I'm not going to list my whole system PATH here, but it includes .\node_modules\.bin at the beginning. Clearly Windows has no problem finding webpack but Node for some reason can't.
I tried running npm run build in a privilege-less Command Line, no difference. I upgraded to the latest versions of Node and npm (13.0.1 and 6.12.1, respectively), to no avail. I even tried prefixing the command in package.json with ./node_modules/.bin/ but that didn't work.
Please don't suggest that I install webpack globally as that is not a real solution. Node is giving me this error with all locally-installed commands, not just webpack. I should be able to run commands locally through npm.
if you're using a recent version of npm, you can try
npx webpack ....
alternatively, you can install webpack locally and specify the exact path
node_modules/webpack/bin/webpack.js ....
In the end, I had to bite the bullet and install webpack globally.

Run knexfile.js from a db/ folder

I have installed the nodejs package globally.
After initializing via knex init and putting the file into a /db folder, commands such as knex migrate:latest won`t work anymore, because knex cannot find the file. Furthermore, knex creates the migrate and seed folder in my root directory
Any suggestions how to config knex that it always looks into the /db folder? Is there any way to do this via a node-script?
Thanks for your reply!
You can give your config file location on script
add this on package.json
"migrate": "yarn knex migrate:latest --knexfile src/config/knexfile.js",
My knexfile is in src/config.
Write your own client implementation which just uses migration API provided by knex.

npm packages not available when installed locally

I am working with npm on a web app and I found an issue when using some packages that requires terminal commands to run such like nodemon and concurrently
I installed it via
sudo npm install --save-dev nodemon
and when I try to use it via:
nodemon ./server.js
I get an error
nodemon command not found
and the same when I used concurrently
I tried also with
sudo npm install --save nodemon
and it doesn't work.
it only work if I installed it globally
sudo npm install -g nodemon
Why I can't use it when install locally?
Note: I can found the executable file at node_modules/.bin
but this following not working as well
node_modules/.bin/nodemon ./server.js
Global packages can be launched directly because they are saved in your PATH directory by default. If you saved a package locally you can see it on node_modules/.bin/ as you mentioned. So there are 2 ways to achieve what you want if you want to run an executable package if installed locally:
You can run it via terminal as ./node_modules/.bin/nodemon yourscript.js
Or via npm scripts in your package.json file, you do this:
{
"scripts": {
"nodemon": "nodemon yourscript.js"
}
}
and execute npm run nodemon.
The 2nd approach works for both packages installed globally or locally.
I prefer installing packages locally, so my other apps won't get affected especially if I'm using different package versions per project.
UPDATE
On npm#5.2.0 onwards, it comes with a binary called npx. So you can run specific packages on the terminal just by npx [package] and it executes either your local or global npm package. In your case it should be something like npx nodemon server.js.
Because it's in your node_modules/.bin folder, not your PATH.
You can either use ./node_modules/.bin/nodemon or $(npm bin)/nodemon to call nodemon.
To run any locally installed npm module (Mocha, Eslint, Nodemon, etc.), you can now use npx. Try npx nodemon server.js.
I also recommend setting main within your package.json to point to the script you want to run (index.js by default), so you could just run npx nodemon or nodemon (if globally installed) and it will know which script to run.
This is because the local node_modules folder is not in your PATH. See the link to the duplicate question for more details.

Testing against multiple node module versions

I'm developing several node modules which extend the functionality of the Sequelize database ORM module. I'm using Travis to run tests before pushing new versions of my modules to NPM.
Travis allows you to run tests against different versions of node (v0.10, v0.12 etc). Similarly, I'd like to be able to run my tests using various different versions of the Sequelize module. i.e. run the tests once using Sequelize 2.0.0, again using 2.0.1, 2.0.2 etc.
Travis loads modules from NPM automatically according the the versions set in the package.json file, and so only runs tests using the latest module version available according to what's specified in the package.json of my module (in this case ^2.0.0).
Is there any way to interfere with this process and get Travis to run the tests multiple times using a different Sequelize version each time?
I've searched for advice on this on Stack Overflow and elsewhere but not had any luck. But I assume I can't be the only person who wants to do this...
In the end the solution I came up with is to put the following in the travis.yml file:
before_script:
- 'if [ "$SEQ_VERSION" ]; then npm install sequelize#^$SEQ_VERSION.0.0; fi'
env:
- SEQ_VERSION=2
- SEQ_VERSION=3
(with thanks to Sandro Munda for his answer which put me on the right track)
In your package.json, you could have some something like this:
...
"scripts": {
"test-sequelize-2.0.0": "npm install sequelize#2.0.0 && ./node_modules/mocha/bin/mocha",
"test-sequelize-2.1.0": "npm install sequelize#2.1.0 && ./node_modules/mocha/bin/mocha",
"test": "npm run test-sequelize-2.0.0 && npm run test-sequelize-2.1.0"
}
...
Then, run npm test.
test-all-versions is a package that will run a command (you want your test script) for a whole range of semantic versions. so if you want to run your tests against sequelize you might write a config file like this.
#.tav.yml
name: sequelize
versions: ^2.0.0
command: npm run test
This will run your npm test script against all published versions of sequelize that match ^2.0.0 (ie 2.0.0, 2.0.1, etc)
Then in your travis config run the tav command to test against all versions in the given range.

Resources