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

Can't self-reference a scoped package using its name #37618

Closed
piranna opened this issue Mar 6, 2021 · 5 comments
Closed

Can't self-reference a scoped package using its name #37618

piranna opened this issue Mar 6, 2021 · 5 comments

Comments

@piranna
Copy link
Contributor

piranna commented Mar 6, 2021

Not sure if it's a Node.js or jest-resolve issue, so I'm cross-posting it.

What steps will reproduce the bug?

At Node.js packages docs it says it's possible to self-reference and import a package from it's own modules by using the package name, the same was as it's done with dependencies installed on node_modules. I'm self-referencing my package itself in my jest tests, it says it can't find it:

   Test suite failed to run

    Cannot find module '@piranna/rpc' from '__tests__/index.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)

Since my package has a scope (package name is in the form with @<scope>/<packageName>) and there's no examples or references in Node.js docs about self-referencing scoped packages, maybe this is a Node.js packages resolving algorythm.

How often does it reproduce? Is there a required condition?

It happens me always. Maybe related to jest-resolve package.

What is the expected behavior?

The self-referenced package should be imported.

What do you see instead?

It give an error stating it can't find the module.

Additional information

I'm trying to do it that way in combination with package.json imports and Jest projects and testEnvironment, I can be able to test my code in multiple environments doing different imports for each one, so I can apply polyfills automatically.

@piranna
Copy link
Contributor Author

piranna commented Mar 6, 2021

Jest issue at jestjs/jest#11161

@targos
Copy link
Member

targos commented Mar 6, 2021

The following example works, so I'd say it's probably a bug in jest-resolve:

package.json:

{
  "name": "@my/package",
  "exports": "./index.js"
}

index.js

module.exports = 42

other.js

console.log(require('@my/package'))
$ node other.js
42

@piranna
Copy link
Contributor Author

piranna commented Mar 6, 2021

The following example works, so I'd say it's probably a bug in jest-resolve

Thanks for confirm it, then now it's a matter of getting a response on their side. I'll open a PR in the docs specifying this use case, since it's not obvious in its current state :-)

I think we can close this now.

@piranna
Copy link
Contributor Author

piranna commented Mar 6, 2021

Pull-request with updated documentation with @targos example available at #37630.

piranna added a commit to piranna/node that referenced this issue Mar 6, 2021
@piranna
Copy link
Contributor Author

piranna commented Mar 6, 2021

Not sure if it's a Node.js or jest-resolve issue, so I'm cross-posting it.

jest-resolve issue is already being addressed in jestjs/jest#10883.

aduh95 pushed a commit that referenced this issue Aug 17, 2021
PR-URL: #37630
Fixes: #37618
Refs: #37618 (comment)
Co-authored-by: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
targos added a commit that referenced this issue Aug 22, 2021
PR-URL: #37630
Fixes: #37618
Refs: #37618 (comment)
Co-authored-by: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
targos added a commit that referenced this issue Sep 4, 2021
PR-URL: #37630
Fixes: #37618
Refs: #37618 (comment)
Co-authored-by: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants