简体   繁体   English

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

[英]Node JS Error: Cannot find module './services'

I am trying to create stubs using sinon to run the test but its throwing error. 我正在尝试使用sinon创建存根以运行测试,但它会引发错误。 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. 它试图找到一个模块服务,并说找不到该模块,但是我在与test相同的文件夹中有services.js。 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 以下是我认为从car-lookup.controller.js导出服务的方式

Below is car lookup controller.js 下面是汽车查找controller.js
If you see in the first line it is trying to import services and services is not a direct js file. 如果您在第一行中看到它正在尝试导入服务,并且服务不是直接的js文件。 I have an index.js file in the ../../directory which is what the first line is referring to. 我在../../目录中有一个index.js文件,这是第一行所指的内容。 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
};

确保正确导出文件服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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