1

hey is it possible to define a module.export with a pretty name like require('my_module') that would help because i don't have to figure out all the '../../' i would have to use to get there.

In my case its about the instance of my i18n helper. I want to do somethink like:

# (module.coffee)

Polyglot = require("node-polyglot")
i18n = new Polyglot()
module.exports = i18n

# ------------

#(view.coffee)

i18n = require('i18n')
i18n.t('my.path.string')

as you see the moudle.coffee is very short and returns an instance of Polyglot class. I don't what to create my own npm_package for this.

I'm just starting out with this hole CommonJS thing, so this could be a pointless question.

Louis
  • 146,715
  • 28
  • 274
  • 320
antpaw
  • 15,444
  • 11
  • 59
  • 88
  • Are you sure you mean to be mentioning RequireJS?? Except for your one mention of it and your use of the tag, I see nothing in your question that pertains to RequireJS. It's all stock Node stuff. (Note, if it need be mentioned: `require(...)` does not entail RequireJS. Although RequireJS does define a `require` call it is not specific to RequireJS.) If you *do* mean to be using RequireJS then please clarify what you plan to do with it in your question. Right now it is completely obscure. – Louis Nov 13 '14 at 10:52
  • I'm pretty sure I'm using stock Node, it's just that this all is very confusing to me and looks like Node uses RequireJS or CommonJS to do the require magic internally. I don't really understand the differences and advantages yet. – antpaw Nov 13 '14 at 11:52
  • Node uses the CommonJS API for modules. You can use RequireJS with Node but that's something you have to do explicitly. (You have to install RequireJS, ask Node to load it, configure it, etc.) I've removed RequireJS mentions from your question since you are not currently using it. – Louis Nov 13 '14 at 11:55

1 Answers1

0

If you are inside a npm module and want to require any other file from the module you should try this syntax:

var submod = require('module/path/to/submod');

As an example you can see the source code of my module simpleS, in index.js I require the host file which is located on the path /lib/http/host in the module location so I use it like this:

var host = require('simples/lib/http/host');

Like this you can use the absolute path of the required file without worrying about the path relative to the current location.

micnic
  • 10,915
  • 5
  • 44
  • 55