New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Globally installed knex Migration CLI path resolution #125

Closed
jwilm opened this Issue Dec 7, 2013 · 4 comments

Comments

Projects
None yet
3 participants
@jwilm

jwilm commented Dec 7, 2013

Running the migration commands (such as knex migrate:make name) using a globally installed knex executable and locally installed sqlite3 fails to resolve 'sqlite3'. Installing knex in the local ./node_modules folder and running a migration with ./node_modules/.bin/knex successfully generates the migration. This is probably a non issue since any application using knex should have a locally installed version, but adding a note in the installation instructions that there are path issues with global installs may help folks avoid this pitfall. Alternatively, if there is value in having a global install, we may wish to address the path errors.

Thanks for your work on Knex and Bookshelf! After with a number of other SQL ORMs for Node, these libraries are a breath of fresh air.

Setup

  • sqlite3 is installed locally in ./node_modules
  • knex is installed globally under /usr/...

Stack Trace

$ knex migrate:make test_migration
Error: Cannot find module 'sqlite3'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/usr/local/lib/node_modules/knex/clients/server/sqlite3.js:8:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
Possibly unhandled TypeError: Cannot read property 'client' of undefined
    at exit (/usr/local/lib/node_modules/knex/lib/cli/migrate.js:107:7)
    at Promise$finally.r (/usr/local/lib/node_modules/knex/node_modules/bluebird/js/main/promise.js:154:62)
    at tryCatch1 (/usr/local/lib/node_modules/knex/node_modules/bluebird/js/main/util.js:85:19)
    at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/usr/local/lib/node_modules/knex/node_modules/bluebird/js/main/promise.js:644:13)
    at Promise$_settlePromiseAt [as _settlePromiseAt] (/usr/local/lib/node_modules/knex/node_modules/bluebird/js/main/promise.js:838:14)
    at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/usr/local/lib/node_modules/knex/node_modules/bluebird/js/main/async.js:63:12)
    at Async$consumeFunctionBuffer (/usr/local/lib/node_modules/knex/node_modules/bluebird/js/main/async.js:36:14)
    at process._tickCallback (node.js:415:13)
@bendrucker

This comment has been minimized.

Collaborator

bendrucker commented Dec 9, 2013

There's been some discussion among the npm team about an npm exec command. You'll notice you don't encounter this problem with "lifecycle scripts" defined in package.json like test because npm puts ./node_modules/.bin in your PATH before running them. The issue here is that none of the client DB libraries are actual dependencies so they don't get installed globally with knex.

The way I deal with this is by including the following line in my bash_profile and then running commands with npm-exec:

alias npm-exec='PATH=$(npm bin):$PATH'

I did add a quick note to the migration CLI docs (bcc9759)

@bendrucker bendrucker closed this Dec 9, 2013

@tgriesser

This comment has been minimized.

Owner

tgriesser commented Dec 9, 2013

Gonna keep this one open until it's added to the actual docs in a new release.

Edit: Forgot that the actual docs just link to the markdown file. Regardless though, I'm wondering if there's something else we can do in the script to resolve to the directory where the script is called as a workaround.

@tgriesser

This comment has been minimized.

Owner

tgriesser commented Dec 12, 2013

Should be updated to hopefully work for most cases when you're operating in the root of the project. Try installing 0.5.1 globally and see if it works.

@tgriesser tgriesser closed this Dec 12, 2013

@bendrucker

This comment has been minimized.

Collaborator

bendrucker commented Dec 13, 2013

The advantage of npm-exec is that npm commands traverse upwards looking for a node_modules folder and will do the same thing no matter what directory you're in. Definitely a good idea to at least have it work from the root dir though. It's definitely possible to replicate npm's behavior but that's overkill.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment