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.

Absolute Imports and aliases feature does not work with Webpack 5 + TypeScript + Yarn PnP

See original GitHub issue

Bug report

Describe the bug

Absolute Imports and aliases feature does not work with Webpack 5 + TypeScript + Yarn PnP. To reproduce this bug, all these three conditions must be met

  1. Use Webpack 5 beta
  2. Use TypeScript
  3. Use Yarn PnP

To Reproduce

mkdir -p pages
cat <<'EOF' > pages/index.tsx
import MyComponent from 'components/MyComponent'

export default function Index() {
  return <MyComponent/>
}
EOF

mkdir -p components
cat <<'EOF' > components/MyComponent.tsx
export default function Index() {
  return <h1>Hi</h1>
}
EOF

cat <<'EOF' > tsconfig.json
{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": ["esnext", "dom", "dom.iterable"],
    "noEmit": true,
    "jsx": "preserve",

    // Strict Typecheck
    "strict": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,

    // Module
    "esModuleInterop": true,
    "moduleResolution": "node",
    "isolatedModules": true,
    "forceConsistentCasingInFileNames": true,

    // Path alias
    "baseUrl": ".",

    // Next.js suggestion
    "allowJs": true,
    "skipLibCheck": true,
    "resolveJsonModule": true
  },
  "exclude": ["node_modules"],
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}
EOF

cat <<'EOF' > package.json
{
  "dependencies": {
    "next": "9.5.3",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "typescript": "^4.0.2"
  },
  "resolutions": {
    "webpack": "^5.0.0-beta.29"
  },
  "devDependencies": {
    "@types/node": "^14.6.4",
    "@types/react": "^16.9.49"
  }
}
EOF

yarn set version berry
yarn install
yarn next dev

Expected behavior

Running webpage

Screenshots

image

ready - started server on http://localhost:3000
event - compiled successfully
wait  - compiling...
event - compiled successfully
event - build page: /
wait  - compiling...
error - ./pages/index.tsx:1:0
Module not found: Your application tried to access components, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.

Required package: components (via "components/MyComponent")
Required by: /home/simnalamburt/workspace/a/pages/

> 1 | import MyComponent from 'components/MyComponent'
  2 |
  3 | export default function Index() {
  4 |   return <MyComponent/>
event - build page: /next/dist/pages/_error
wait  - compiling...
error - ./pages/index.tsx:1:0
Module not found: Your application tried to access components, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.

Required package: components (via "components/MyComponent")
Required by: /home/simnalamburt/workspace/a/pages/

> 1 | import MyComponent from 'components/MyComponent'
  2 |
  3 | export default function Index() {
  4 |   return <MyComponent/>
wait  - compiling...
event - compiled successfully

System information

  • OS: Debian GNU/Linux 10
  • Version of Next.js: 9.5.3, 9.5.4-canary.3
  • Version of Node.js: 14.9.0
Reference

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
simnalamburtcommented, Sep 11, 2020

If I shouldn’t use this feature, this behavior should be removed from the document.

2020년 9월 11일 (금) 05:22, Kristoffer K. notifications@github.com님이 작성:

You shouldn’t use path mapping like that, it’s better to use the link protcol as that is tool agnostic https://yarnpkg.com/features/protocols#why-is-the-link-protocol-recommended-over-aliases-for-path-mapping

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vercel/next.js/issues/16892#issuecomment-690705139, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABB235OXJJM2OHELUIFEBQ3SFEYO5ANCNFSM4Q44LXRA .

3reactions
sokracommented, Sep 7, 2020

It’s a webpack bug about handling yarn pnp

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack is not working with Typescript aliases - Stack Overflow
You have specified path aliases for typescript compiler but you also need to specify aliases for webpack cause webpack resolves modules in ...
Read more >
Webpack 5 release (2020-10-10)
Webpack 5 adds a new feature called "Module Federation", which allows multiple webpack builds to work together. From runtime perspective modules ...
Read more >
Clean Up Your Imports using Absolute Imports & Alias on ...
I'm using an ejected CRA and typescript. It's been awhile but I believe all I did was set the baseUrl to ./src in...
Read more >
ts-loader | Yarn - Package Manager
The simple solution is to disable it by using the transpileOnly: true option, but doing so leaves you without type checking and will...
Read more >
Changelog - Cypress Documentation
Fixed an issue with Angular Component Testing where a custom sourceRoot configuration would not be respected. Fixes #24827. Fixed Typescript typings for cy....
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