Skip to content
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

Trying to use with koa #1

Closed
gx0r opened this issue Jul 13, 2015 · 5 comments
Closed

Trying to use with koa #1

gx0r opened this issue Jul 13, 2015 · 5 comments

Comments

@gx0r
Copy link

gx0r commented Jul 13, 2015

I put the following in node_modules/koa/lib/application.js

var co = require('co');

const Promise = require('bluebird');
Promise.longStackTraces();
require('bluebird-co')
co.wrap = function(fn) {
    return Promise.coroutine(fn);
}

is this the proper approach?

@novacrazy
Copy link
Owner

Basically, you have to make sure every one of your dependencies is requiring the same instance of Bluebird, and then you can just require bluebird-co from a script before you use it. You don't need to edit any module files directly, ever.

The easiest way to do that is by making sure your application has bluebird (and co in this case) in its dependencies before you install anything else.

So you'd create your package.json via running npm init in your project directory or manually creating it, then run npm install bluebird co bluebird-co --save.

Then run npm install koa koa-router anything-else-koa-related --save, and so forth. Now when those install, they will use the pre-existing copy of bluebird and co that you installed manually.

Then create a file index.js that looks something like:

var Promise = require('bluebird');
var co = require('co');

//Optionally enable long stack traces for debugging
Promise.longStackTraces();

require('bluebird-co');

co.wrap = function(fn) {
    return Promise.coroutine(fn);
}

//Optionally use bluebird Promises globally
global.Promise = Promise;

require('./app.js'); //start your application

You should never have to manually edit other libraries code.

However, if you have already populated your package.json with many libraries, you can achieve the desired dependency state by running npm install bluebird co bluebird-co --save to save them to your package.json, then rm -rf ./node_modules to delete all your dependencies, and finally run npm install and it will reinstall all your modules, using the same copy of Bluebird and co for every dependency.

@gx0r
Copy link
Author

gx0r commented Jul 13, 2015

Ah! That makes sense, got it working now. Thanks!

@gx0r gx0r closed this as completed Jul 13, 2015
@novacrazy
Copy link
Owner

It's just one of the quirks of NPM and node_modules. If you're not careful you can end up with many copies of the same library being used around your application.

@novacrazy
Copy link
Owner

I'm actually going to reopen this issue in case anyone else has trouble integrating bluebird-co into their stack. It's a subtle problem that I can't really fix, but getting it right really pays off.

@novacrazy novacrazy reopened this Jul 14, 2015
@novacrazy
Copy link
Owner

This thread isn't as applicable now that I've switched to using Bluebird as a peer dependency and koa has/is moved/moving away from generators, so I'm going to close it.

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

No branches or pull requests

2 participants