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

Browserify cannot find runnable module when using require('mocha') #1316

Open
taylor-smith opened this issue Aug 20, 2014 · 27 comments
Open

Browserify cannot find runnable module when using require('mocha') #1316

taylor-smith opened this issue Aug 20, 2014 · 27 comments

Comments

@taylor-smith
Copy link

@taylor-smith taylor-smith commented Aug 20, 2014

When compiling test scripts into a single monolith using Browserify, I hit the error:

module "./runnable" not found from "../node_modules/mocha/mocha.js".

Line 846 of mocha.js requires runnable as such:

var Runnable = require('./runnable');

Does this need to be amended as such?

var Runnable = require('./lib/runnable');

If this is true, it seems there are several file paths that need to be renamed. Is there a workaround?

@boneskull
Copy link
Member

@boneskull boneskull commented Aug 26, 2014

@taylor-smith How are you require()-ing Mocha?

@zzswang
Copy link

@zzswang zzswang commented Aug 26, 2014

same problem when using browserify

@chrisdevereux
Copy link

@chrisdevereux chrisdevereux commented Sep 3, 2014

Same problem with browserify. Am just using require('mocha') from within a browserify module.

@xcoderzach
Copy link

@xcoderzach xcoderzach commented Sep 4, 2014

I think the issue is that mocha has it's own internal require system. which is relative to lib, not the root dir.

@boneskull
Copy link
Member

@boneskull boneskull commented Sep 5, 2014

I think the issue is that Browserify uses its own require system. The only thing "weird" Mocha does is use relative paths, which is not weird at all.

In a NodeJS context, require('mocha') should find the root index.js, which requires everything else.

In a Browserify context, require('mocha') seems to require the root mocha.js. My suggestion is to require the root index.js, however that is accomplished.

I'm a little confused why you're bundling Mocha with your app, but I don't have much experience with Browserify.

@boneskull boneskull closed this Sep 5, 2014
@nevir
Copy link

@nevir nevir commented Sep 11, 2014

Yeah, browserify isn't smart about it - if you ask it to require('mocha/index.js'), browserify gets tripped up on the conditional requires (it tries to load lib-cov/* which doesn't exist in a vanilla checkout)

@boneskull
Copy link
Member

@boneskull boneskull commented Sep 13, 2014

@nevir Hoping to resolve that issue soon. I'd like to get Mocha built w/ Browserify instead of the current homebrew solution.

@nevir
Copy link

@nevir nevir commented Sep 13, 2014

I wouldn't hold your breath. I get the sense that it's a lot of work on
either side.
On Fri, Sep 12, 2014 at 11:30 PM Christopher Hiller <
notifications@github.com> wrote:

@nevir https://github.com/nevir Hoping to resolve that issue soon. I'd
like to get Mocha built w/ Browserify instead of the current homebrew
solution.


Reply to this email directly or view it on GitHub
#1316 (comment).

@boneskull
Copy link
Member

@boneskull boneskull commented Sep 13, 2014

@nevir Not sure why I'd hold my breath or not; I'll implement it instead.

@nevir
Copy link

@nevir nevir commented Sep 13, 2014

Ah, gotcha
On Sat, Sep 13, 2014 at 11:26 AM Christopher Hiller <
notifications@github.com> wrote:

@nevir https://github.com/nevir Not sure why I'd hold my breath or not;
I'll implement it instead.


Reply to this email directly or view it on GitHub
#1316 (comment).

@dy
Copy link

@dy dy commented Nov 25, 2014

Is there a solution to this issue yet? I still get Cannot find module './lib-cov/mocha' or Cannot find module './runnable' if I browserify mocha/mocha.js or mocha/index.js.

Is there a workaround? I’m trying to concat: browserify -x mocha -r assert -r ./index.js:../index | cat - ./node_modules/mocha/mocha.js > test/test.bundle.js and still get the same.

@wejrowski
Copy link

@wejrowski wejrowski commented Jun 16, 2015

Same problem. Trying to run mocha as a stand alone in a nashorn environment.

@ehgoodenough
Copy link

@ehgoodenough ehgoodenough commented Oct 25, 2015

Still getting this issue, both on browserify and webpack.

Error: Cannot find module './lib-cov/mocha' from '/Users/Andrew/Projects/bananabomber/node_modules/mocha'
Error: Cannot find module './lib-cov/jade' from '/Users/Andrew/Projects/bananabomber/node_modules/mocha/node_modules/jade'
Error: Cannot find module 'sass' from '/Users/Andrew/Projects/bananabomber/node_modules/mocha/node_modules/jade/lib'
Error: Cannot find module 'stylus' from '/Users/Andrew/Projects/bananabomber/node_modules/mocha/node_modules/jade/lib'
Error: Cannot find module 'less' from '/Users/Andrew/Projects/bananabomber/node_modules/mocha/node_modules/jade/lib'
Error: Cannot find module 'markdown' from '/Users/Andrew/Projects/bananabomber/node_modules/mocha/node_modules/jade/lib'
Error: Cannot find module 'markdown-js' from '/Users/Andrew/Projects/bananabomber/node_modules/mocha/node_modules/jade/lib'
Error: Cannot find module 'discount' from '/Users/Andrew/Projects/bananabomber/node_modules/mocha/node_modules/jade/lib'
Error: Cannot find module 'marked' from '/Users/Andrew/Projects/bananabomber/node_modules/mocha/node_modules/jade/lib'
Error: Cannot find module 'coffee-script' from '/Users/Andrew/Projects/bananabomber/node_modules/mocha/node_modules/jade/lib'
@dy
Copy link

@dy dy commented Dec 21, 2015

@taylor-smith Hello! I wonder why this issue is closed, the problem is still actual. Are there no plans on making mocha browserifyable?
Same as #1260 and #880

@strugee
Copy link

@strugee strugee commented Feb 22, 2016

@dfcreative if you look through the conversation, @taylor-smith wasn't the one who closed the issue. @boneskull did.

@boneskull
Copy link
Member

@boneskull boneskull commented Feb 23, 2016

@dfcreative @taylor-smith @strugee

Mocha bundles itself with browserify. You need to include node_modules/mocha/mocha.js in your bundle. I believe this can be accomplished by various means depending on your use case.

@boneskull
Copy link
Member

@boneskull boneskull commented Feb 23, 2016

(require('mocha') gives you ./node_modules/mocha/index.js which is not what you want)

@dy
Copy link

@dy dy commented Feb 23, 2016

@boneskull yes, I know, it is difficult to understand why mocha uses browserify to build it’s own browser mocha.js, but does not provide browser entry compatible with other packages.
I tried to fork mocha and make it browserifyable, but the change was quite big and inpredictable that it would took months to merge it with no guarantees, as the mocha is quite big and serious runner.

So I just gave up and wrote tst, a minimalistic and fast mocha-compatible replacement for node and browser.

@boneskull
Copy link
Member

@boneskull boneskull commented Feb 23, 2016

@dfcreative If it's possible to provide a browser entry which specifies mocha.js and also use it for replacing modules, then we could do that--though I don't think it is? Seems strange. Alternatively, we could simply leverage a package like aliasify for the module replacement and use browser to specify mocha.js.

Seems fairly straightforward, unless I'm missing something.

I also don't quite understand the use case for browserifying Mocha itself. Can someone explain this?

@boneskull
Copy link
Member

@boneskull boneskull commented Feb 23, 2016

I see this (from wejrowski)

Same problem. Trying to run mocha as a stand alone in a nashorn environment

But it's not clear what this means.

@dy
Copy link

@dy dy commented Feb 24, 2016

@boneskull browserifying mocha would be useful with browserify servers for dev, like beefy or budo.

@boneskull
Copy link
Member

@boneskull boneskull commented Feb 24, 2016

@dfcreative Aren't these tools usually used for running your app, not your tests (is that their intent)?

@dy
Copy link

@dy dy commented Feb 24, 2016

@boneskull no, they are only for development tasks afaik

@boneskull
Copy link
Member

@boneskull boneskull commented Feb 24, 2016

Well, yes, they are not intended for production use. I meant they are for running your app in "development" mode.

I can infer from beefy's docs that it's meant to serve a directory, ostensibly with .html files in it.

I'd recommend using something like karma-mocha and karma-browserify to run tests. You can use the following bit of configuration to get the HTML reporter to display in the browser (using karma-chrome-launcher or equivalent):

// karma config object
{
  frameworks: ['browserify', 'mocha'],
  files: ['src/**/*.js', 'test/**/*.js'],
  preprocessors: {
    '**/*.js': ['browserify']
  },
  browserify: {
    debug: true
    // plus whatever else; package.json settings will be read as well
  },
  reporters: ['progress'], // or whatever console reporter you wish (or none?)
  client: {
    mocha: {
      reporter: 'html'
    }
  },
  autoWatch: true, // reruns on file change
  singleRun: false, // "karma start" will run tests + stay open
  browsers: ['Chrome']
}

To use this, create your karma.conf file, use this configuration in it, and run karma start (install karma-cli globally if you want karma in your PATH). An instance of Chrome will launch (separate from one which may be currently running). Click the "Debug" button in the window, and a new window will appear containing the HTML reporter output.

That being said, I'll try to see if the browser field supports what we want to do here. Reopening.

@dy
Copy link

@dy dy commented Feb 24, 2016

running your app in "development" mode.

@boneskull yes, exactly, thanks for understanding. That would be nice mocha to be as easy as var test = require('mocha');

@boneskull
Copy link
Member

@boneskull boneskull commented Feb 24, 2016

yes, making test files "node-able" is going to be the most basic usage of mocha at some point here

@stale
Copy link

@stale stale bot commented Oct 17, 2017

I am a bot that watches issues for inactivity.
This issue hasn't had any recent activity, and I'm labeling it stale. In 14 days, if there are no further comments or activity, I will close this issue.
Thanks for contributing to Mocha!

@stale stale bot added the stale label Oct 17, 2017
@boneskull boneskull added help wanted and removed stale labels Oct 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
10 participants
You can’t perform that action at this time.