react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Some NPM packages don't work

Open bmcmahen opened this issue 11 years ago • 16 comments

I've come across a few npm modules that don't appear to work correctly. Notably, event-emitter has issues importing es5-ext/object/assign, and debug doesn't appear to work with the chrome dev-tools. I suspect that small alterations to the module themselves (to take into account yet another working environment) may be necessary, but there could also be issues with the packager, too.

bmcmahen avatar Mar 28 '15 17:03 bmcmahen

I can't get it work with lodash too.

var _ = require('lodash'); and var lodash = require('lodash'); both return an empty object.

Take a look at the bundle file generated by packager, the code for lodash is there.

novadev94 avatar Mar 28 '15 19:03 novadev94

I've also had a hard time getting some modules to work. I was able to get lodash to work by using the lodash-node version. But, things like Backbone are not.

It looks like the packager thinks the exported function is a factory for creating the module, but the factory (in lodash) is not actually detecting a node environment and instead the export ends up being undefined.

I'll keep digging in the bundle and report if I find anything useful.

jgable avatar Mar 28 '15 20:03 jgable

I had to use the lodash-cli to build a node version for React: lodash exports=node

nicinabox avatar Mar 28 '15 21:03 nicinabox

It looks like, at least in the case of backbone (and probably regular underscore/lodash), that it checks for a define && define.amd to determine whether to use AMD versus commonJS style exports.

Why does the packager set define.amd if it's preferring commonJS exports? Is it just because that's what the polyfill had initially? Can we remove that?

I guess, the follow up question would be, if we do allow AMD style defines, why aren't they working for lodash, et. al.

jgable avatar Mar 28 '15 21:03 jgable

@nicinabox The lodash-node package on npm should work for you without making a custom build; at least it has for me so far. npm install lodash-node --save and then var _ = require('lodash-node');.

jgable avatar Mar 29 '15 00:03 jgable

I think it's been deprecated though.

nicinabox avatar Mar 29 '15 00:03 nicinabox

When I comment out this define.amd = {} I am able to load lodash and backbone (well, backbone is a little trickier because it depends on underscore) without a problem.

jgable avatar Mar 29 '15 01:03 jgable

For the amd issue, here's our code responsible for it. https://github.com/facebook/react-native/blob/master/packager/react-packager/src/DependencyResolver/haste/polyfills/require.js#L597 I'm unsure why we define this global.

vjeux avatar Mar 29 '15 01:03 vjeux

Woops @jgable beat me to it!

vjeux avatar Mar 29 '15 01:03 vjeux

package he shows same issue.

    // Some AMD build optimizers, like r.js, check for specific condition patterns
    // like the following:
    if (
        typeof define == 'function' &&
        typeof define.amd == 'object' &&
        define.amd
    ) {
        define(function() {
            return he;
        });
    }   else if (freeExports && !freeExports.nodeType) {
        if (freeModule) { // in Node.js or RingoJS v0.8.0+
            freeModule.exports = he;
        } else { // in Narwhal or RingoJS v0.7.0-
            for (var key in he) {
                has(he, key) && (freeExports[key] = he[key]);
            }
        }
    } else { // in Rhino or a web browser
        root.he = he;
    }

siuying avatar Mar 29 '15 07:03 siuying

I have the same issue with async,

var async = require('async');
console.log(_);// {}

OlivierFortin avatar Mar 29 '15 22:03 OlivierFortin

fixed via #581 will publish v0.3.2 soon

amasad avatar Apr 01 '15 16:04 amasad

Naive question alert, but why not support AMD?

broonage avatar Aug 20 '15 14:08 broonage

@broonage: we're building a new js environment (others are browser, web worker, node) and need to figure out what we support. We want to support as few things as possible so that it is easier to maintain and has less bugs overall. It turns out that the community pretty much settled on commonjs and not AMD so this is what we chose to implement. Having two things to do the same thing is usually not a great idea.

If in the future AMD makes a big comeback and there are big incentives to implement it, we could. But trying to keep it as simple as possible for now :)

vjeux avatar Aug 20 '15 15:08 vjeux

@vjeux: Thanks, good to know. I'm trying to re-use some existing web modules in a project which use AMD (define), so I have to edit all the files so they are working with require() calls and module.exports=CoolReactNativeModule. I guess no other way about it. :) Thanks again.

broonage avatar Aug 20 '15 20:08 broonage

@cpojer would suggest you to write a codemod for this :p

vjeux avatar Aug 20 '15 21:08 vjeux