configuring requirejs reading from node_modules

GTDev picture GTDev · May 8, 2012 · Viewed 14.7k times · Source

I'm trying to setup a nodejs project to use requirejs. I call my program with node r.js ./config/main.js and my main.js looks like the following:

var cs = require("coffee-script");
var requirejs = require("requirejs");

requirejs.config({
  nodeRequire: require,
  baseUrl: ".",
  paths: {
    cs: "cs",
    CoffeeScript: "CoffeeScript",
    csBuild: "csBuild",

    express: "express",
    nohm: "nohm",
    redback: "redback",
    _: "underscore",
    "connect-redis": "connect-redis",
    freebase: "freebase"
   }
});

console.log("hetet");
requirejs(["cs!./config/app"], function(app){
  console.log("closing")
});

and inside app.coffee:

define((require) ->

  express = require("express")

  RedisStore = require("connect-redis")(express)
  app = express.createServer()

  config = require('cs!./config')

  require('cs!./setup')(app, express, RedisStore)
  require('cs!./routes')(app)
  require('cs!../src/server')

  app.listen(config.server.port)
)

I seem to fail in main.js with the error:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: Calling node's require("config") failed with error: Error: Calling node's require("config") failed with error: Error: Cannot find module 'config'

and What I have noticed is when I comment out the line var requirejs = require("requirejs"); (in main.js), I get further and fail at the line RedisStore = require("connect-redis")(express) (in app.coffee) with the error:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
TypeError: undefined is not a function
    at ./config/app.coffee:10:41

I have been having a lot of trouble configuring requirejs in node any help would be appreciated.

thanks

Answer

jrburke picture jrburke · May 10, 2012

It is best to not configure requirejs to look in node_modules, since modules in that area are modules formatted for node. There is a little bit more info in the requirejs node page.