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

Closed
taylor-smith opened this issue Aug 20, 2014 · 28 comments
Closed
Labels
status: no reproduction We couldn't figure out how to reproduce this. Please post a standalone issue. status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior type: bug a defect, confirmed by a maintainer

Comments

@taylor-smith
Copy link

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
Contributor

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

@zzswang
Copy link

zzswang commented Aug 26, 2014

same problem when using browserify

@chrisdevereux
Copy link

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

@xcoderzach
Copy link

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
Contributor

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.

@nevir
Copy link

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
Contributor

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

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

@nevir
Copy link

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

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

@ehgoodenough
Copy link

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 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 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
Contributor

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

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

@dy
Copy link

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
Contributor

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

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 commented Feb 24, 2016

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

@boneskull
Copy link
Contributor

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

@dy
Copy link

dy commented Feb 24, 2016

@boneskull no, they are only for development tasks afaik

@boneskull
Copy link
Contributor

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.

@boneskull boneskull reopened this Feb 24, 2016
@boneskull boneskull added type: bug a defect, confirmed by a maintainer and removed Unconfirmed labels Feb 24, 2016
@dy
Copy link

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
Contributor

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

@stale
Copy link

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 this has been inactive for a while... label Oct 17, 2017
@boneskull boneskull added status: accepting prs Mocha can use your help with this one! and removed stale this has been inactive for a while... labels Oct 17, 2017
@JoshuaKGoldberg
Copy link
Member

This issue has been open and accepted for well over 5 years and hasn't seen a PR. Also:

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

Browserify is, at this point, relatively legacy software. It's not something we're going to put a lot of effort into supporting. Closing this issue as wontfix / aged away. Someone please yell at me if I'm wrong here.

...and if you do, please post a recent isolated reproduction for how to demonstrate this issue. None of us on the team use Browserify regularly that I know of. 😄

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg added status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior and removed status: accepting prs Mocha can use your help with this one! labels Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg added the status: no reproduction We couldn't figure out how to reproduce this. Please post a standalone issue. label Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: no reproduction We couldn't figure out how to reproduce this. Please post a standalone issue. status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

No branches or pull requests