【问题标题】:How to call variables and function from outside of a module in NestJS?如何从 NestJS 模块外部调用变量和函数?
【发布时间】:2022-12-03 16:16:05
【问题描述】:

我在/src/common/helper/cash.helper.ts 中有一些辅助函数。当我从模块调用此函数时,出现以下错误。

Error: Cannot find module './../../../src/common/helper/cast.helper' Require stack:

但是,e2e 测试没有任何问题。在这里,您可以看到文件夹结构。

当我将导入更改为绝对路径import { toNumber } from 'src/common/helper/cast.helper'时;它在工作,但 e2e 测试不工作。

这里出了什么问题?如何在 NestJS 的所有模块中使用通用函数和常量?

【问题讨论】:

  • 不要使用绝对路径。否则你需要在 jest 配置文件中设置 moduleNameMapper
  • 你应该可以做 src/common/helper/cast.helper 不?

标签: node.js nestjs


【解决方案1】:

我做了以下来解决这个问题。

  1. 我把导入改为绝对路径导入{ toNumber } from 'src/common/helper/cast.helper'
  2. 为了修复 e2e 测试,我已将 "moduleDirectories": ["<rootDir>/../", "node_modules"] 添加到 jest-e2e.json。

【讨论】:

    猜你喜欢
    • 2014-02-21
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多