Module did not self-register. #86

Closed
freder opened this Issue Mar 12, 2015 · 9 comments

Projects

None yet

10 participants

@freder
freder commented Mar 12, 2015

hi,

I'm not sure if it's a gulp-mocha problem per se, but it happens when I'm running test with gulp-mocha, so I'm posting it here.

gulpfile.js

var source_file_pattern = path.join(src_dir, '**/*.js');
var test_file_pattern = path.join(test_dir, '*.js');

gulp.task('mocha', function() {
    return gulp.src(test_file_pattern, { read: false })
        .pipe(mocha())
        .on('error', function(err) {
            notifier.notify({
                title: err.plugin,
                message: err.message
            });
        });
});

gulp.task('watch', function() {
    gulp.watch([source_file_pattern, test_file_pattern], ['mocha']);
});

gulp.task('default', ['watch']);

./test/test.js

var libxml = require('libxmljs');

describe('validation', function() {
    it('should be a valid document', function() {
        //
    });
});

so when I run gulp, it watches for file changes and runs the test once something changes. the first time the tests run fine, the second time this happens:

[12:35:03] Using gulpfile /Volumes/Macintosh HD 2/Projects/project/code/project.js/gulpfile.js
[12:35:03] Starting 'watch'...
[12:35:03] Finished 'watch' after 14 ms
[12:35:03] Starting 'default'...
[12:35:03] Finished 'default' after 13 μs
[12:35:22] Starting 'mocha'...


  validation
    ✓ should be a valid document


  1 passing (22ms)

[12:35:22] Finished 'mocha' after 129 ms
[12:35:24] Starting 'mocha'...
[12:35:24] 'mocha' errored after 35 ms
[12:35:24] Error in plugin 'gulp-mocha'
Message:
    Module did not self-register.
Stack:
Error: Module did not self-register.
    at Error (native)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at bindings (/Volumes/Macintosh HD 2/Projects/project/code/project.js/node_modules/libxmljs/node_modules/bindings/bindings.js:76:44)
    at Object.<anonymous> (/Volumes/Macintosh HD 2/Projects/project/code/project.js/node_modules/libxmljs/lib/bindings.js:1:99)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)

so this seems to have something to do with libxmljs (or its dependencies). however, when I do mocha --watch, I don't get the error.

versions:

% npm ls | ag '(gulp[^-]|mocha|libxmljs|bindings)'
├─┬ gulp@3.8.11
├─┬ gulp-mocha@2.0.0
├─┬ libxmljs@0.13.0
│ ├── bindings@1.2.1
├─┬ mocha@2.1.0

% node -v
v0.12.0

any ideas?

@freder freder referenced this issue in TooTallNate/node-bindings Mar 12, 2015
Closed

Module did not self-register. #17

@noamokman

Same here,
I'm working on a native module and this only pops up on my iojs tests.

Not using libxmljs, but I do use bindings

Node 0.12 works fine with the same package.

iojs tests output:

node --version
v1.5.1
npm --version
2.7.0
npm test

> random-project@0.0.0 test C:\projects\random-project
> gulp test

[17:09:26] Using gulpfile C:\projects\random-project\gulpfile.js
[17:09:26] Starting 'jshint'...
[17:09:26] Starting 'mocha'...
[17:09:27] 'mocha' errored after 235 ms
[17:09:27] Error in plugin 'gulp-mocha'
Message:
    Module did not self-register.
Stack:
Error: Module did not self-register.
    at Error (native)
    at Module.load (module.js:339:32)
    at Function.Module._load (module.js:294:12)
    at Module.require (module.js:349:17)
    at require (module.js:368:17)
    at bindings (C:\projects\random-project\node_modules\bindings\bindings.js:76:44)
    at Object.<anonymous> (C:\projects\random-project\lib\file\index.js:5:12)
    at Module._compile (module.js:444:26)
    at Object.Module._extensions..js (module.js:462:10)
    at Module.load (module.js:339:32)
[17:09:27] Finished 'jshint' after 437 ms
npm ERR! Test failed.  See above for more details.
Command exited with code 1
@kevva kevva closed this Mar 22, 2015
@AlexeyGrishin

I just met same problem, and proposed solution (reinstall npm modules) did not help - when I use watch + gulp-mocha and my code depends on some lib with 'bindings' library the tests are running fine on the first run, but after any change they fall with the provided error.

I found a workaround: gulp-mocha clears 'require' cache on each run except modules loaded before 'gulp-mocha'. In my project I use 'bcrypt' library which depends on 'bindings', so I just put it to my gulpfile.js:

var gulp = require('gulp')
    , bcrypt = require('bcrypt')
    , mocha = require('gulp-mocha')

Hope that helps.

@rockymadden

I have observed this same issue when runnings tests against react components. The offending library is jsdom/contextify.

@justingreenberg

@AlexeyGrishin elegant solution 👍

thank you

@outdooricon

Ended up needing to add require('jsdom'); before my require('gulp-mocha') in gulpfile.js as hinted at by @AlexeyGrishin and @rockymadden.

@wordijp wordijp added a commit to wordijp/frontend-base-project that referenced this issue May 6, 2015
@wordijp wordijp testが正常に動いてなかったのを修正
jsdomのnode.jsサポートは3.xまで

Module did not self-registerの修正
  ->応急処置として、require('jsdom')をgulp-mochaのロード前にして対応
  ->sindresorhus/gulp-mocha#86
d85265e
@elchudi
elchudi commented May 7, 2015

Thanks @AlexeyGrishin , also the same thing is happening with zmq node library, and loading it in the tests before gulp-mocha solved the problem

@superman16885

Thanks @AlexeyGrishin, I was using bcrypt in my sequelize user model for password hashing and was facing the same issue with gulp watch. Adding var bcrypt = require('bcrypt') before requiring gulp-mocha fixed the issue.

@buildmaster buildmaster added a commit to hoist/gulp-mocha that referenced this issue Jun 2, 2015
@buildmaster buildmaster hack to fix Module did not self-register errors
it seems that removing .node references from the require cache leads to
issues with and error "Module did not self-register". Hence why if you
use bcrypt or anything you end up having to order your requires and
putting modules into your gulpfile that aren't needed in your gulp
script.

This is a temporary fix but it works
ref: sindresorhus/gulp-mocha #86
6f55b94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment