question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when resolving es6 module using paths in TypeScript code

See original GitHub issue

🐛 Bug Report

The following TypeScript code

import * as employeeData from 'Source_Data/employee';

generates this error message

    Cannot find module 'Source_Data/employee' from 'src/framework/module/header.ts'

    Require stack:
      src/framework/module/header.ts
      src/framework/backend/jsonData.ts
      test/unittest/backend/jsonData.ts

      1 | import {uuid} from 'Source_ODS/uuid';
      2 | //import * as employeeData from 'Source_Framework/data/employee';
    > 3 | import * as employeeData from 'Source_Data/employee';
        | ^
      4 |
      5 |
      6 | /**

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)
      at Object.<anonymous> (src/framework/module/header.ts:3:1)

when using this paths settings:

{
  "extends": "./configs/tsconfig_base",
  "compilerOptions": {
    "paths": {
      "Source_Modules/*": ["./src/modules/*"],
      "Source_Framework/*": ["./src/framework/*"],
      "Source_Data/*": ["./src/framework/data/*"],
      "Source_ODS/*": ["./src/framework/ods/*"],
      "Source_Vendor/*": ["./vendor/*"],
      "CoreImg/*": ["./img/*"],
      "jquery-ui/*": ["jquery-ui/ui/*"],
      "jquery-ui-css/*": ["./vendor/jquery-ui/ui-lightness/*"],
      "JQueryUIImg/*": ["./vendor/jquery-ui/ui-lightness/images/*"]
    }
  },
  "compileOnSave": false,
  "include": [
    "src/**/*",
    "test/unittest/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

but when changing it to this

import * as employeeData from 'Source_Framework/data/employee';

it works as expected.

To Reproduce

The problem can be reproduced at will but I’m currently not able to extract it from a large production environment into a simple example.

Expected behavior

The code reporting an error should work the same way the workaround works as the module does exist.

Link to repo (highly encouraged)

Unfortunately not yet possible but working on it

Debug log:

ts-jest.log

envinfo

System:
    OS: macOS 10.15.7
    Node version: 14.15.0

Npm packages:
    jest: 26.6.3
    ts-jest: 26.4.4
    typescript: 4.0.5
    babel(optional):

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
ahnpnlcommented, Nov 8, 2020

If one is using TypeScript path mapping in the project and want to use jest, moduleNameMapper must be configured for jest to understand. It is like a bridge between TypeScript and Jest.

ts-jest provides 2 ways to help to make it easier to configure:

0reactions
doberkoflercommented, Nov 8, 2020

@ahnpnl Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error when resolving es6 module using paths in TypeScript ...
The code reporting an error should work the same way the workaround works as the module does exist. Link to repl or repo...
Read more >
Documentation - Module Resolution
Module resolution is the process the compiler uses to figure out what an import ... import can be resolved relative to baseUrl ,...
Read more >
eslint / typescript: Unable to resolve path to module
The import plugin doesn't know about the correct location of the TypeScript config and hence cannot resolve those paths.
Read more >
Typescript – How to solve the problem with unresolved path ...
Generally, what should be done can be summarized as follows: Take a look into tsconfig.json and check if there are path aliases defined...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
json contains an "exports" field, in which case files within packages can only be accessed via the paths defined in "exports" . For...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found