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.

webpack5 resolve file error in monorepo

See original GitHub issue

Bug report

What is the current behavior? I use webpack 5 with this plugin in a simple yarn workspaces monorepo, Which contain a simple add and a common util library. But there is an error reported: image

If the current behavior is a bug, please provide the steps to reproduce. This is a repo to reproduce: test-monorepo.

  1. git clone https://github.com/diem98/test-monorepo.git
  2. cd test-monorepo && yarn
  3. cd apps/web-app
  4. yarn dev

After that, you can see the error in the figure above. apps/web-app/src/App.tsx content as below:

import React, {useState} from 'react';
import {addOne} from 'common-tools';
import { add } from './utils';

console.log(`add(1,2) : ${add(1,2)}`)

const App = () => {
  const [num, setNum] = useState(0);
  
  const handleClick = () => {
    setNum(prev => addOne(prev));
  }

  return (
    <div>
      <h1>current number: {num}</h1>
      <button type="button" onClick={handleClick}>addOne</button>
    </div>
  )
}

export default App;

You can see that import addOne from common-tools package and import add function from it’s own utils directory.

packages/common-tools export detail as below: image

But inside the dist, it is like this: image image

You will find that import ./utils in common-tools/src/add.ts compiled to ./src/utils/index.js, This leads to the error described above.

What is the expected behavior?

It should compile successfully as it did in webpack 4.

branch webpack-4 will compile successfully with the same code.

I tried to dive into the plugin source code, The getInnerRequest return value was found to be different image

In webpack 4 ./utils just returns as expected: ./src/utils. But In webpack 5, ./utils returns src/utils. So in webpack 5 it will execute the following matchPath function. then ./utils request in common-tools/src/add.ts resolved to apps/web-app/src/utils/index.js.

I found that the different results are related to the different versions of the enhanced-resolve implementation. But I don’t know if it was done intentionally.

image

https://codesandbox.io/s/focused-rain-4hoyq?file=/src/index.js

Thank you very much for your help, and please forgive my broken English.

Other relevant information: webpack version: 5.32 Node.js version: 14.15.4 Operating System: macos

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:8

github_iconTop GitHub Comments

8reactions
alexander-akaitcommented, Apr 19, 2021

I will look at this in near future

6reactions
Hartoeftcommented, Sep 14, 2021

Any news on this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack resolution error when working in monorepo mode
It seems that webpack tries to resolve optional dependencies. I have not included @nestjs/microservices in any of my packages but is shows up...
Read more >
Module not found: Error: Can't resolve in babel and typecript in ...
I create a lerna monorepo (nodejs packages) with and I try to compile my typescript files with babel and webpack.
Read more >
Resolve | webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
error: can't resolve 'fs' in | The AI Search Engine You Control
mix.webpackConfig({ node: { fs: 'empty' }}). to your webpack.mix.js file. Open side panel. NUXT: Module not found: Error: Can't resolve 'fs'.
Read more >
tsconfig-paths-webpack-monorepo-plugin - npm
Start using tsconfig-paths-webpack-monorepo-plugin in your ... Notice that the plugin is placed in the resolve.plugins section of the ...
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