Search code examples
node.jsnestjs

How to call variables and function from outside of a module in NestJS?


I have some helper functions inside the /src/common/helper/cash.helper.ts. When I call this function from a module, I get the following error.

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

However, the e2e tests are working without any problem. Here, you can see the folder structure.

When I change the import to absolute path import { toNumber } from 'src/common/helper/cast.helper'; It's working, but the e2e tests are not working.

enter image description here

What's wrong here? How can I use common functions and constants across all the modules in NestJS?


Solution

  • I did the following to fix the issue.

    1. I changed the import to absolute path import { toNumber } from 'src/common/helper/cast.helper';
    2. To fix the e2e test, I have added "moduleDirectories": ["<rootDir>/../", "node_modules"] to the jest-e2e.json.