jasmine-node icon indicating copy to clipboard operation
jasmine-node copied to clipboard

Can not require modules from node_modules

Open theotow opened this issue 10 years ago • 7 comments

Hello,

i cant get this working:

modules/testRequire.js

define(function () {
    return require("express");
});

setup.js

var requirejs = require('requirejs');

requirejs.config({
    nodeRequire: require,
    baseUrl: "/Users/Manuel/Dropbox/Synced/Git/require/modules"
});

tests/sampleSpec.js

requirejs(["testRequire"],function(test){
    describe("check if object", function () {
        it("hallo ",function(){
            waitsFor(function() {
                return expect(test).toEqual(jasmine.any(Object));
            }, "The Value should be incremented", 750);
        });
    });
});

it run:

jasmine-node --requireJsSetup setup.js --captureExceptions /Users/Manuel/Dropbox/Synced/Git/require/tests

and get Error: Tried loading "express" at /Users/Manuel/Dropbox/Synced/Git/require/modules/express.js then tried node's require("express") ....

any ideas why it cant find express in /Users/Manuel/Dropbox/Synced/Git/require/node_modules ?

Thx a lot

theotow avatar Sep 15 '13 11:09 theotow

I believe it considers the test file the root so it will look adjacent and down the directory for a modules directory. Try using relative pathing to look up using ./../ and ./../ combinations

davidposin avatar Sep 15 '13 17:09 davidposin

But why should someone have different modules for testing? Where should i use relative pathes?

theotow avatar Sep 15 '13 20:09 theotow

Require will take a relative path. so require(./../modules/express) should look up one directory. Can you change modules back to node_modules? Node, and it's various modules, expect that hard coded directory to be there and it may be easier if you use that directory.

davidposin avatar Sep 16 '13 14:09 davidposin

I dont get what you mean in any way, sry.

I thing is if i run the same code without jasmine-node and instead use node like that:

node app.js

app.js

var requirejs = require('requirejs');

requirejs.config({
    nodeRequire: require,
    baseUrl: "/Users/Manuel/Dropbox/Synced/Git/require/modules"
});

requirejs( ['testRequire'], function( express ) {
    console.log(express);
});

it works without error, it seams like the jasmine-node replaces the require, and uses wrong pathes...

theotow avatar Sep 16 '13 20:09 theotow

This should be marked as a bug since it does not conform to the normal Node behavior. Node looks at current directory and then traverses up, not down.

xixixao avatar Oct 31 '13 17:10 xixixao

Yes. I agree this is a bug. It looks in the global jasmine-node directory (and parents) instead of node_modules itself. To test this out for yourself, try

define([
  'gaze'
  ],function(gaze) {
  return "anything";
});

I use gaze in my example because I don't use that package in my own app but it's a dependency for jasmine-node.

For me, it looks through

 paths: 
  [ '/usr/local/lib/node_modules/jasmine-node/bin/node_modules',
    '/usr/local/lib/node_modules/jasmine-node/node_modules',
    '/usr/local/lib/node_modules',
    '/usr/local/node_modules',
    '/usr/node_modules',
    '/node_modules' ] },

azhang avatar Nov 01 '13 23:11 azhang

The way to solve this issue is to install jasmine-node locally, not -g, and run it from there (node_modules/jasmine-node/bin/jasmine-node). That way, it will search ./node_modules, ../node_modules, ../../node_modules, etc. and eventually find your app's node_modules dir.

azhang avatar Nov 02 '13 07:11 azhang