简体   繁体   中英

Node JS Error: Cannot find module './services'

I am trying to create stubs using sinon to run the test but its throwing error. Its trying to find a module services and saying the module is not found but I have the services.js in the same folder as test. So I am not sure why its failing. Can someone please advise what is wrong in the code.

 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() }
    });
  });


Below is how I think the services are being exported from the car-lookup.controller.js

Below is car lookup controller.js
If you see in the first line it is trying to import services and services is not a direct js file. I have an index.js file in the ../../directory which is what the first line is referring to. Directory structure is also below. Please advise.

>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
};

确保正确导出文件服务

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM