Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest-resolver unable to resolve relative require in dependency #7570

Closed
robertsmieja opened this issue Dec 31, 2018 · 7 comments
Closed

jest-resolver unable to resolve relative require in dependency #7570

robertsmieja opened this issue Dec 31, 2018 · 7 comments
Labels

Comments

@robertsmieja
Copy link

🐛 Bug Report

When jest-resolver attempts to load a dependency that uses a relative 'require', it fails to resolve.

Here is the code in ./node_modules/discord.js/index.js :

...
  // Utilities
...
  version: require('../package').version,
...

package.js exists at ./node_modules/discord.js/package.js

To Reproduce

Steps to reproduce the behavior:

git clone https://github.com/robertsmieja/jest-resolver-relative-import
npm install
npm run test

Expected behavior

This is the expected error message:

  ● Test suite failed to run

    Cannot find module '../package' from 'index.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)

Link to repl or repo (highly encouraged)

https://github.com/robertsmieja/jest-resolver-relative-import

Run npx envinfo --preset jest

Paste the results here:

PS D:\Users\Robert\Documents\GitHub\jest-resolver-relative-import> npx envinfo --preset jest
npx: installed 1 in 2.348s

  System:
    OS: Windows 10
    CPU: (8) x64 Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
  Binaries:
    Node: 10.14.2 - D:\Program Files\nodejs\node.EXE
    Yarn: 1.12.3 - C:\Users\Robert\AppData\Roaming\npm\yarn.CMD
    npm: 6.4.1 - D:\Program Files\nodejs\npm.CMD
@robertsmieja robertsmieja changed the title jest-resolver unable to resolve relative import in dependency jest-resolver unable to resolve relative require in dependency Dec 31, 2018
@robertsmieja
Copy link
Author

I narrowed this down a bit:
This issue occurs with both JavaScript and TypeScript, but if you remove the "jest.config.js" it runs only the JavaScript test without issues.

@SimenB
Copy link
Member

SimenB commented Jan 2, 2019

The issue is that it's not package.js, it's package.json.

If I upgrade your repo to Jest 24, the error becomes clearer:

  ● Test suite failed to run

    Cannot find module '../package' from 'index.js'

    However, Jest was able to find:
        '../package.json'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['ts', 'tsx', 'js'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:200:17)

Adding json solves your issue.


However, I don't like that the error says "from index.js" - that's really hard to find. We should include the actual require line in the stack trace

@SimenB SimenB closed this as completed Jan 2, 2019
@robertsmieja
Copy link
Author

Thanks for taking the time to reply and explain the issue.

I didn't notice it ended in .json

@ksac1
Copy link

ksac1 commented Jul 17, 2019

What is the solution for this issue?

@robertsmieja
Copy link
Author

As per the updated error message, I updated my jest.config.js file to have the following:

    // An array of file extensions your modules use
    moduleFileExtensions: [
        "ts",
        "tsx",
        "js",
        "jsx",
        "json",
    ],

@SimenB
Copy link
Member

SimenB commented Jul 17, 2019

Improving the error a bit more here: #8704

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants