javascript - Cannot find module - relative paths -


i wrote simple local module manage few lines of localized text. uses node's require load language files, i'm having trouble paths, likely. i'm getting cannot find module error.

file structure

. +-- local_modules |   +-- lang |   |   +-- package.json |   |   +-- index.js +-- locale |   +-- en.js |   +-- de.coffee +-- init.js +-- index.coffee +-- package.json 

relevant module code

should require file if not loaded.

join = require('path').join; _config.path = './locale'; lang = 'en'; _locales = {};  if(!_locales[lang]){     _locales[lang] = require(join(_config.path, lang)); } 

every file in locale directory typical node.js module, example en.js:

module.exports = {     test: 'hello world!' }; 

the local module exports function(req, res, next){}, used express middleware , supposed attach required object localized strings onto res.locals, however, i'm seeing cannot find module 'locale/en' error.

i've tried manually add .js extensions (but shouldn't neccessary far know). have tried different variations on path, such locale or /locale.


the module called in index.coffee.
app launched using init.js, contains following:

require('coffee-script/register'); require('./index'); 

maybe it's module .js (and module doesn't have coffeescript dependency) can not load .coffee file? although coffeescript should registered globally, or wrong? either way, doesn't work .js file either, guess has paths.

path.join() normalizes created path, (probably) means ./ part removed, , remained relative path.

instead, when path.resolve() used, creates absolute path, needed in case.


Comments