2015-08-14 5 views
0

У меня есть файл, который, я считаю, должен быть импортирован как модуль, но при попытке импортировать его в основной файл (см. Main.js), я получаю следующую ошибку:Модуль узла не распознается как модуль

Error: Cannot find module 'sessionStore.js' 

Я уверен, что файл находится в правильном месте. Любые идеи, что еще может вызвать это?

main.js

var express = require('express'); 
var bodyParser = require('body-parser'); 
var util = require('util'); 
var toMarkdown = require('to-markdown'); 
var jsdiff = require('diff'); 
var marked = require('marked'); 
var pg = require('pg'); 
//need to get sessions to work and use server credentials instead of password 
var sessionStore = require('sessionStore.js'); 
var PodioJS = require('podio-js').api; 
var podio = new PodioJS({authType: 'password', clientId: "xxx", clientSecret: "xxx" },{sessionStore:sessionStore}); 

sessionStore.js

var fs = require('fs'); 
var path = require('path'); 

function get(authType, callback) { 
    var fileName = path.join(__dirname, 'tmp/' + authType + '.json'); 
    var podioOAuth = fs.readFile(fileName, 'utf8', function(err, data) { 

    // Throw error, unless it's file-not-found 
    if (err && err.errno !== 2) { 
     throw new Error('Reading from the sessionStore failed'); 
    } else if (data.length > 0) { 
     callback(JSON.parse(data)); 
    } else { 
     callback(); 
    } 
    }); 
} 

function set(podioOAuth, authType, callback) { 
    var fileName = path.join(__dirname, 'tmp/' + authType + '.json'); 

    if (/server|client|password/.test(authType) === false) { 
    throw new Error('Invalid authType'); 
    } 

    fs.writeFile(fileName, JSON.stringify(podioOAuth), 'utf8', function(err) { 
    if (err) { 
     throw new Error('Writing in the sessionStore failed'); 
    } 

    if (typeof callback === 'function') { 
     callback(); 
    } 
    }); 
} 

module.exports = { 
    get: get, 
    set: set 
}; 

ответ

3

вы пробовали использовать относительные пути? Если они находятся в той же директории, то

var sessionStore = require('./sessionStore'); 

без .js

+0

Это было исправление, я не могу поверить, что я этого не понимал! Благодаря! –

3

ошибка, вы получаете, потому что узел не может найти свой sessionStore модуль в каталоге node_modules.

If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then node starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location.

https://nodejs.org/api/modules.html#modules_file_modules

Вы, вероятно, хотите использовать относительный путь к файлу. Что-то вроде: require('./sessionStore')

A module prefixed with '/' is an absolute path to the file. For example, require('/home/marco/foo.js') will load the file at /home/marco/foo.js.

A module prefixed with './' is relative to the file calling require(). That is, circle.js must be in the same directory as foo.js for require('./circle') to find it.

Смежные вопросы