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

Issue when requiring is.js with webpack #100

Closed
jonrh opened this issue Mar 24, 2015 · 16 comments
Closed

Issue when requiring is.js with webpack #100

jonrh opened this issue Mar 24, 2015 · 16 comments

Comments

@jonrh
Copy link

jonrh commented Mar 24, 2015

Just came across a weird issue when trying to require is.js when using the module bundler webpack. In JavaScript I do:

var is = require("is_js");

When using webpack this causes webpack to freak out and give an error message like this:

ERROR in ./~/is_js/is.js
Module not found: Error: Cannot resolve module 'is' in C:\jonrh\isjs-webpack-bugExample\node_modules\is_js
 @ ./~/is_js/is.js 8:8-13:10

I'm not entirely sure where the fault is but I suspect it has something to do with is.js and AMD module loading. Maybe even webpack.

I created a repository with some sample code and documentation to reproduce this issue. Included in the repository is a very hacky way to prevent this issue.

@jaroslav-muller
Copy link

I'm having the issue as well.
What works for webpack is replacing line 8 with define(['./is'], function(is) {
(['is'] replaced by a relative path ['./is']).

This way webpack pretends to be a amd loader and work well. The old way tries to locate module 'is' in your modules path instead of simply getting file 'is.js' in current dir.

Another way to workaround it, is to add '.' to resolve.modulesDirectories in your webpack config. But the consequences might not all be welcome...

@jaroslav-muller
Copy link

Checking the UMD recommendation mentioned in e6c2b0c, I now believe that it was applied wrong. Their example https://github.com/umdjs/umd/blob/master/returnExportsGlobal.js, which seems to be basis of the implementation, shows how to create a module that depend on another module (b in their case).

The actual is_js implementation is like defining module 'is', which depends on module 'is'. The factory function ignores the parameter is anyway.

The is.js file should start like this (removing the obsolete dependency, removing parameter is and declaring it as a var in line 33):

// is.js 0.7.3
// Author: Aras Atasaygin

// AMD with global, Node, or global
;(function(root, factory) {
    if(typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(function() {
            // Also create a global in case some scripts
            // that are loaded still are looking for
            // a global even when an AMD loader is in use.
            return root.is = factory();
        });
        // root.is = factory();
    } else if(typeof exports === 'object') {
        // Node. Does not work with strict CommonJS, but
        // only CommonJS-like enviroments that support module.exports,
        // like Node.
        module.exports = factory();
    } else {
        // Browser globals (root is window)
        root.is = factory();
    }
} (this, function() {

    // Baseline
    /* -------------------------------------------------------------------------- */

    var root = this || global;
    var previousIs = root.is;

    // define 'is' object and current version
    var is = {};
    is.VERSION = '0.7.3';

@sporto
Copy link

sporto commented Mar 29, 2015

Same issue here

@davidmerrique
Copy link

@jonrh @jaroslav-muller @sporto Take a look here for a workaround #41

module: {
  loaders: [
    { test: /node_modules/, loader: "imports?define=>undefined" }
  ]
}

@jonrh
Copy link
Author

jonrh commented Apr 1, 2015

@davidmerrique Thanx! Do you know if there could be any side effects from this workaround by doing this AMD exclusion?

@dfguo
Copy link

dfguo commented Apr 3, 2015

+1 on this issue

@davidmerrique
Copy link

@jonrh I don't think so, unless you're somehow requiring a package that's only AMD.

@Semigradsky
Copy link

Same issue. @davidmerrique workaround broke me React but this chnages helped me:

module: {
  loaders: [
    { test: /is_js/, loader: "imports?define=>undefined" }
  ]
}

@rubenspgcavalcante
Copy link

rubenspgcavalcante commented Apr 7, 2015

I'm having a building error because of that. :/
Because of the line 8, when running amd-optmizer, gives a error:
'Circular dependency detected. Module 'is' has been processed before.'
It's because when it is optmized the module 'is' requires itself as dependencie. Only works if removing the 'is' dependencie of it

define(['is'], function(is) {

to

define([], function() {

@eriklharper
Copy link

+1 for me as well. I was able to get this to work also by changing the AMD dependency is to `is_js' like so:

if(typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['is_js'], function(is) {
            // Also create a global in case some scripts
            // that are loaded still are looking for
            // a global even when an AMD loader is in use.
            return (root.is = factory(is));
        });
    } 

@eriklharper
Copy link

#100 (comment) > This also worked for me as well

@necolas
Copy link

necolas commented May 15, 2015

@arasatasaygin do you have any collaborators that could help you resolve this issue?

@arasatasaygin
Copy link
Owner

@necolas Nope, there is no PR for this one, and it effects a lot of people I guess.
Opened an issue #132

@chiefGui
Copy link

chiefGui commented Jun 4, 2015

You all seems to be using AMD with Webpack and experiencing this issue. I am using CommonJS (Webpack) and also experiencing it—seems it doesn't belongs only to AMD users. Just registering. :-)

@arasatasaygin
Copy link
Owner

This is fixed with #138
Thanks for patience and all the help.

@jonrh
Copy link
Author

jonrh commented Jan 4, 2016

Thanks @arasatasaygin, @ryantemple, and others for your work, excellent job!

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

No branches or pull requests