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

Absolute imports do not work when Yarn PnP is enabled #7996

Closed
yohanliyanage opened this issue Nov 18, 2019 · 11 comments
Closed

Absolute imports do not work when Yarn PnP is enabled #7996

yohanliyanage opened this issue Nov 18, 2019 · 11 comments

Comments

@yohanliyanage
Copy link

yohanliyanage commented Nov 18, 2019

I created a React App using create-react-app with PnP enabled.

npx create-react-app pnp --use-pnp

Then following the documentation, I created a jsconfig.json file to enable absolute imports.

https://create-react-app.dev/docs/importing-a-component/#absolute-imports

 {
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

When I use absolute imports, the compiler fails to resolve those. Changing the imports to have relative paths work. Just to see whether this is PnP related, I deleted the '.pnp.js' file and removed the relevant config from package.json as well. Then it worked fine. So I assume this is PnP related.

Environment: create-react-app v3.2.0 on Mac OS X Catalina

@hlian
Copy link

hlian commented Nov 25, 2019

Can reproduce! Related issue: #7047

I get this error when I run yarn webpack-cli --config webpack.dev.js:

ERROR in ./main.tsx 19:0-30
Module not found: Error: Something that got detected as your top-level application (because it doesn't seem to belong to any package) tried to access a package that is not declared in your dependencies

Required package: src (via "src/api")

I'm not using create-react-app, but the setup is similar: I want PnP to realize that TypeScript knows how to resolve these paths.

@hlian
Copy link

hlian commented Nov 28, 2019

For people coming from search engines, I think this is the issue to subscribe to: yarnpkg/berry#589

@stale
Copy link

stale bot commented Dec 28, 2019

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Dec 28, 2019
@yohanliyanage
Copy link
Author

@hlian Thanks for the confirmation and sharing the related issue with regards to Typescript. However, I see this issue with regular JavaScript as well. It seems yarnpkg/berry#589 is Typescript specific.

@stale stale bot removed the stale label Dec 29, 2019
@Gallevy
Copy link

Gallevy commented Jan 26, 2020

Any update on this? main breaking point for upgrade to yarn berry

@nickserv
Copy link
Contributor

Yarn 2/berry already fixed this, you just need to tell it how to resolve each module in src using link: at the beginning of a dependency. https://next.yarnpkg.com/advanced/migration#dont-use-resolvealiasmodules-webpack-or-modulenamemapper-jest

@Felix-Indoing
Copy link

This may work in a single repo, but not working with typescript + monorepo

@stale
Copy link

stale bot commented Mar 29, 2020

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Mar 29, 2020
@nickserv
Copy link
Contributor

It seems like this should work with Yarn 2 as long as workspaces aren’t used yarnpkg/berry#589 (comment)

@stale stale bot removed the stale label Mar 29, 2020
@stale
Copy link

stale bot commented Apr 28, 2020

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Apr 28, 2020
@stale
Copy link

stale bot commented May 3, 2020

This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.

@stale stale bot closed this as completed May 3, 2020
@lock lock bot locked and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants