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 and aliases feature does not work with Webpack 5 + TypeScript + Yarn PnP #16892

Closed
simnalamburt opened this issue Sep 6, 2020 · 8 comments · Fixed by webpack/enhanced-resolve#245

Comments

@simnalamburt
Copy link
Contributor

simnalamburt commented Sep 6, 2020

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
@simnalamburt simnalamburt changed the title Absolute Imports and aliases feature does not work with Webpack 5 Absolute Imports and aliases feature does not work with Webpack 5 + TypeScript Sep 6, 2020
@simnalamburt simnalamburt changed the title Absolute Imports and aliases feature does not work with Webpack 5 + TypeScript Absolute Imports and aliases feature does not work with Webpack 5 + TypeScript + Yarn PnP Sep 6, 2020
@simnalamburt simnalamburt changed the title Absolute Imports and aliases feature does not work with Webpack 5 + TypeScript + Yarn PnP Absolute Imports and aliases feature does not work with Webpack 5 + TypeScript 4 + Yarn PnP Sep 6, 2020
@simnalamburt simnalamburt changed the title Absolute Imports and aliases feature does not work with Webpack 5 + TypeScript 4 + Yarn PnP Absolute Imports and aliases feature does not work with Webpack 5 + TypeScript + Yarn PnP Sep 6, 2020
@simnalamburt
Copy link
Contributor Author

simnalamburt commented Sep 6, 2020

I just checked webpack configs and could confirmed that resolve key was properly modified. Looks like those configs are being ignored in Webpack 5 + TypeScript + Yarn PnP in some reason.

module.exports = {
  webpack: config => {
    console.log(config.resolve.modules)
    console.log(config.resolve.plugins)
    return config
  }
}

@sokra
Copy link
Member

sokra commented Sep 7, 2020

It's a webpack bug about handling yarn pnp

@timneutkens timneutkens added this to the 9.x.x milestone Sep 7, 2020
@Timer Timer modified the milestones: 9.x.x, backlog Sep 7, 2020
@merceyz
Copy link
Contributor

merceyz commented Sep 10, 2020

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

@simnalamburt
Copy link
Contributor Author

simnalamburt commented Sep 11, 2020 via email

@jensmeindertsma
Copy link

Does someone have a link to the related issue on the Webpack repository ( as it is a Webpack-side problem )?

@simnalamburt
Copy link
Contributor Author

webpack/enhanced-resolve#245 has been merged, I'll try this again and close this if it's fixed.

@simnalamburt
Copy link
Contributor Author

Resolved in Webpack 5.0.0 and Next.js 9.5.5. @sokra Thanks for your effort!!

@Timer Timer removed this from the backlog milestone Nov 16, 2020
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants