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

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

Comments

Projects
None yet
10 participants
@taylor-smith

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

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Aug 26, 2014

Member

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

Member

boneskull commented Aug 26, 2014

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

@zzswang

This comment has been minimized.

Show comment
Hide comment
@zzswang

zzswang Aug 26, 2014

same problem when using browserify

zzswang commented Aug 26, 2014

same problem when using browserify

@chrisdevereux

This comment has been minimized.

Show comment
Hide comment
@chrisdevereux

chrisdevereux Sep 3, 2014

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

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

@xcoderzach

This comment has been minimized.

Show comment
Hide comment
@xcoderzach

xcoderzach 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.

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

@boneskull

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Sep 5, 2014

Member

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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@nevir

nevir 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)

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

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Sep 13, 2014

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@nevir

nevir 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).

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

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Sep 13, 2014

Member

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

Member

boneskull commented Sep 13, 2014

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

@nevir

This comment has been minimized.

Show comment
Hide comment
@nevir

nevir 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).

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

This comment has been minimized.

Show comment
Hide comment
@dy

dy 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.

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

This comment has been minimized.

Show comment
Hide comment
@wejrowski

wejrowski Jun 16, 2015

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

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

@ehgoodenough

This comment has been minimized.

Show comment
Hide comment
@ehgoodenough

ehgoodenough 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'

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

This comment has been minimized.

Show comment
Hide comment
@dy

dy 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

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

This comment has been minimized.

Show comment
Hide comment
@strugee

strugee Feb 22, 2016

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

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

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Feb 23, 2016

Member

@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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Feb 23, 2016

Member

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

Member

boneskull commented Feb 23, 2016

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

@dy

This comment has been minimized.

Show comment
Hide comment
@dy

dy 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.

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

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Feb 23, 2016

Member

@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?

Member

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

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Feb 23, 2016

Member

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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@dy

dy Feb 24, 2016

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

dy commented Feb 24, 2016

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

@boneskull

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Feb 24, 2016

Member

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

Member

boneskull commented Feb 24, 2016

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

@dy

This comment has been minimized.

Show comment
Hide comment
@dy

dy Feb 24, 2016

@boneskull no, they are only for development tasks afaik

dy commented Feb 24, 2016

@boneskull no, they are only for development tasks afaik

@boneskull

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Feb 24, 2016

Member

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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@dy

dy 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');

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

This comment has been minimized.

Show comment
Hide comment
@boneskull

boneskull Feb 24, 2016

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@stale

stale bot 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 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