节点JS错误:找不到模块'./services'

时间:2019-08-19 15:42:36

标签: node.js proxyquire

我正在尝试使用sinon创建存根以运行测试,但它会引发错误。 它试图找到一个模块服务,并说找不到该模块,但是我在与test相同的文件夹中有services.js。所以我不确定为什么它会失败。 有人可以告诉我代码中有什么问题吗?

 1) the car-lookup controller "before each" hook for "should return filtered TAP response":
     Error: Cannot find module './services' from 'C:\nodejsworkspace\olive\server\api\car-lookup'
      at Function.module.exports [as sync] (node_modules\proxyquire\node_modules\resolve\lib\sync.js:40:15)
      at Proxyquire._resolveModule (node_modules\proxyquire\lib\proxyquire.js:137:20)
      at Proxyquire.<anonymous> (node_modules\proxyquire\lib\proxyquire.js:205:35)
      at Array.reduce (<anonymous>)
      at Proxyquire._withoutCache (node_modules\proxyquire\lib\proxyquire.js:204:6)
      at Proxyquire.load (node_modules\proxyquire\lib\proxyquire.js:129:15)
      at Context.<anonymous> (test\unit\server\api\car-lookup\car-lookup.controller.test.js:30:18)

    controller = proxyquire('../../../../../server/api/car-lookup/car-lookup.controller.js', {
      './services': { taClient: new MockTaClient() }
    });
  });


以下是我认为从car-lookup.controller.js导出服务的方式

下面是汽车查找controller.js
如果您在第一行中看到它正在尝试导入服务,并且服务不是直接的js文件。我在../../目录中有一个index.js文件,这是第一行所指的内容。目录结构也在下面。请指教。

>server
  > api
    > car-lookup
      >car-lookup.controller.js

>server 
 >services 
   >index.js
'use strict';
const services = require('../../services');
const { ApiError, ValidationError } = require('../../errors');
const AccountModel = require('../../models/account-model');

const lookupAccount = (req, res, next) => {

  const retrieveAccount = (oktaToken) => { 
    return services.tapClient.retrieveAccount(req.body);
  };



  const sendResponse = (account) => {
    res.send(new AccountModel(account));
  };

  const onError = (err) => {
    next(err instanceof ValidationError ?
      new ApiError(err, 400) :
      err);
  };

  retrive()
    .then(retrieveAccount)
    .then(sendResponse)
    .catch(onError);
};

module.exports = {
  lookupAccount
};

1 个答案:

答案 0 :(得分:0)

确保正确导出文件服务