Absolute Imports and aliases feature does not work with Webpack 5 + TypeScript + Yarn PnP
See original GitHub issueBug 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
- Use Webpack 5 beta
- Use TypeScript
- 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
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:
- Created 3 years ago
- Comments:8 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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님이 작성:
It’s a webpack bug about handling yarn pnp