Cannot find module 'curl/plugin/wire' & specs module's path

51 views
Skip to first unread message

Alessandro Giansanti

unread,
Aug 10, 2014, 8:01:40 AM8/10/14
to cuj...@googlegroups.com
Hi all, i'm gettin my feet wet with CujoJs, and i decided to start with wire and curl-amd in nodejs env..
first of all: i read https://github.com/cujojs/wire/blob/master/docs/README.md just twice, but deeply, and i understood quite well the concepts.. 
so..
i have my dir containing:
+-node_modules
| +-wire
| +-curl
| +-when
+-adder.js
+-pipeliner.js
+-app_wire.js
+-app_curl.js
+-testspec.json

adder.js and pipeliner.js are modules,
 i won't show their simple code as the issue isn't there..

i'm trying to wire the simple testspec.js.
app_wire.js tries to do it with wire api
while curl_wire.js tries to do it with curl api and wire plugin

app_wire.js:
var spec = require('./testspec');
var wire = require('wire');
wire
(spec).then(function (ctx) {
  console
.log(ctx.pipe.run(2));
});


curl_wire.js
var curl = require('curl-amd');
curl
(['wire!./testspec']).then(function (ctx) {
  console
.log(ctx.pipe.run(2));
});

well i succeed to make app_wire.js work structuring 
testspec.json this way:
{
 
"adder3": {
   
"create": {
     
"module": "../../adder",
     
"args": 3
   
}
 
},
 
"adder4": {
   
"create": {
     
"module": "../../adder",
     
"args": 4
   
}
 
},
 
"pipe": {
   
"module": "../../pipeliner",
   
"properties": {
     
"run": {
       
"compose": [{
         
"$ref": "adder3"
       
}, {
         
"$ref": "adder4"
       
}]
     
}
   
},
   
"ready": "run"
 
}
}


first issue: the wire finds the modules only with those trialing ../../ instead of a ./ that i would expect..
but wiring works great.

second issue: 
starting curl_wire.js the console outputs: 
 { [Error: Cannot find module 'curl/plugin/wire'] code: 'MODULE_NOT_FOUND' }

i tried some curl.config with paths, but noway..

Thank you,
.. and ..
Cujojs looks reeeeal great !!

Alessandro Giansanti

unread,
Aug 10, 2014, 10:18:39 AM8/10/14
to cuj...@googlegroups.com
ok i did a little step further on the second issue:
now i config'd curl this way:
curl.config({
  packages: [{
    name: 'when',
    location: 'when',
    main: 'when'
  }, {
    name: 'wire',
    location: 'wire',
    main: 'wire'
  }]
});

and running curl_wire.js  the wire! plugin is found(i think .. that same error is not logged at least..)
anyway now i have a
[Error: Module not resolved: testspec]
i tried to add various trialing ../ but didn't help..

Alessandro Giansanti

unread,
Aug 10, 2014, 6:00:24 PM8/10/14
to
Anyway the main issue is: 
when wiring a spec, the modules' id(path) seem to be relative to wire.js path instead of the current spec path as i would expect ... 
what am i missing?

Alessandro Giansanti

unread,
Aug 10, 2014, 7:45:00 PM8/10/14
to cuj...@googlegroups.com
I managed the main issue:
in app_wire.js calling wire('/' + __dirname + '/testspec') with the absolute spec's path as arg, all referenced modules' id are considered relative to the spec itself..
and it works also with spec $importing multiple sub-specs   
So the question is: is it the correct way? 

Il giorno domenica 10 agosto 2014 14:01:40 UTC+2, Alessandro Giansanti ha scritto:

unscriptable

unread,
Aug 11, 2014, 10:28:02 AM8/11/14
to cuj...@googlegroups.com
Hey Allesandro,

Thanks for the details!  Much appreciated.

There's a subtle difference between `wire(require('path/to/spec'))` and `wire('path/to/spec')`.  While the first one seems like the better way, wire can't know how to normalize relative ids since it only has an object.  It has no path information.  You may as well have done this: `wire({ /* a spec */ });`, right?

Here are some solutions.  

1. Use `wire('path/to/spec')`.
2. Use `wire('path/to/spec', require)` or `wire(require('path/to/spec'), require)`.
3. Use `require` inside your specs instead of module paths.

Option 1 is the simplest, of course.  Wire uses its own `require` to normalize ids based on the path you give it.

Option 2 allows you to pass a require function into wire.  Wire will use this function to resolve module ids.  Therefore, relative module ids will be normalized according to the module where you captured the reference to `require`.  For instance, if you capture `require` in app_wire like this:

var req = require;
wire
(spec, req).then(function (ctx) {
  console
.log(ctx.pipe.run(2));
});


then wire will normalize ids against app_wire (essentially, the root of your project).  

To use option 3, you may have to switch from JSON format to CommonJS format.  Here's what it looks like:

module.exports = {
 
"adder3": {
   
"create": {
     
"module": require("./adder"),

     
"args": 3
   
}
 
},
 
"adder4": {
   
"create": {

     
"module": require("./adder"),
     
"args": 4
   
}
 
},
 
"pipe": {
   
"module": require("./pipeliner"),

   
"properties": {
     
"run": {
       
"compose": [{
         
"$ref": "adder3"
       
}, {
         
"$ref": "adder4"
       
}]
     
}
   
},
   
"ready": "run"
 
}
}

Notice the `module.exports = `.  You will have to rename this module to testspec.js, rather than testspec.json.

Please try these options to see which you prefer.

Regarding curl.js.  I believe you will have to configure curl with a baseUrl that points at the root of your project.  Then configure the packages locations as "node_modules/curl", "node_modules/wire", "node_modules/when", etc.  (Yes, you should configure curl's package, too!)

Let us know how these solutions work for you -- and which ones you prefer!

Thanks!

-- John

Alessandro Giansanti

unread,
Aug 14, 2014, 10:10:40 AM8/14/14
to
Thank you John!
well, at the beginning i thought that wire was going to resolve ids sniffing some process.env  ...
but that would obviously not have been a flexible solution..

My opinion about the 3 options you suggest:
2nd one is most flexible.. 
1st is most straightforward,
3rd one is the last resort.

as i think that the beauty of wire is the chance to build a complete app architecture in a declarative way!
that's why i begun (and will try always to) declaring specs in JSON!

you know, 
i've been attempting to start a veeery simple, personal, "wire-ish" module because i really wished to separate architecture from implementation, 
I've been so happy when i found cujo/wire.. 
and so surprised too, because i found many features and how tos that i was trying to achieve!

i can't wait to get into all other cujo modules too:
cola : seems powerful but it is lacking for docs, or did i miss them?
jiff : awesome :)
meld : great in combination with wire!!
msgs : didn't read enough to grab the potentiality

anyway ..

i'll take the chance here to put a couple of Qs:
- i didn't try yet as theese days i'm moving(home): my sample is flattened in the same dir, but i believe that after the first main wire call, 
  all the subsequent module ids are resolved against the preceeding(the caller's) module path in a require flavour, true?
- curl-amd is supposed to be used especially for remote specs?

Thank you!
Alec

Alessandro Giansanti

unread,
Aug 14, 2014, 11:24:24 AM8/14/14
to cuj...@googlegroups.com
ok, John, i'm testing now the 2nd option..
i found something that doesn't stand:
on my main(first) wire call:

wire('./spec', require);

this is wire function:
function wire(spec, options) {

// If the root context is not yet wired, wire it first
if (!rootContext) {
rootContext = createContext(rootSpec, null, rootOptions);
}

// Use the rootContext to wire all new contexts.
return rootContext.then(function (root) {
return root.wire(spec, options);
});
}
context is my require,
but rootContext is undefined here,
so it goes for the "default" createContext and it doesn't find ./spec ...
!!!


Il giorno lunedì 11 agosto 2014 16:28:02 UTC+2, unscriptable ha scritto:

Alessandro Giansanti

unread,
Aug 15, 2014, 9:57:31 AM8/15/14
to cuj...@googlegroups.com
Ok, got it..
sorry John, i should dig more through github code ;)
var req = require;
wire
('path/to/spec',{require:req});


Il giorno lunedì 11 agosto 2014 16:28:02 UTC+2, unscriptable ha scritto:

unscriptable

unread,
Aug 15, 2014, 10:08:18 AM8/15/14
to cuj...@googlegroups.com
Oops!  Sorry for the bad information! :(  Glad you got it working.

unscriptable

unread,
Aug 15, 2014, 10:11:11 AM8/15/14
to cuj...@googlegroups.com
- curl-amd is supposed to be used especially for remote specs?

curl-amd is meant to be used to load AMD modules and use AMD plugins in node.  There are several solutions for loading AMD *modules* in node (including transpilers), but not very many solutions for using AMD *plugins*.

-- John
Il giorno lunedì 11 agosto 2014 16:28:02 UTC+2, unscriptable ha scritto:
Reply all
Reply to author
Forward
0 new messages