How to fix " Module not found: Can't resolve 'fs' "
Published: 2023-10-25 07:04:20

## **ALERT**
(Revised 10-29-2023）
It might have been caused by [Aggregating modules](https://developer.mozilla.org/en-US/dtwitter.com/notdetailsocs/Web/JavaScript/Reference/Statements/export)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/552712/953fab67-0084-0d71-6950-36b49440e231.png)

I was using `export * from 'module-name'` in the above figure when the error was occurring.

I changed it as follows

```js
export * from 'module-name'
// ↓
export { name1 } from "module-name"
```

---


## **ALERT**

The content of this article was incorrect. (Revised on 2023-10-26)

I found that the error in the title was only reproduced under very specific circumstances. 

Here is the explanation of the situation and the resolved method.

---

Since the issue did not occur in the pure (fully from scratched) Next.js code, I reexamined the code of the affected application.

Although only a part of the error screen was described in this article, here is the full content:

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/552712/af06ed7f-cd4e-a70d-1f8b-78cf4fd2a43a.png)

I traced the Import Trace in the figure (which I did not pay attention to before).

I had previously created a barrel file called `remarkWrapper.ts` to consolidate external modules.

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/552712/cefc1a8e-45c0-a253-a17b-502d21beca52.png)

After `remarkWrapper.ts`, the import had changed to `node_modules`. Therefore, I suspected that the import of that file might be causing the issue, so I removed the import to `index.ts`.

```ts
// index.ts

export * from './getOembed'
export * from './getDataTheme'
// export * from './renderReact'
export * as rehype from './rehypeWrapper'
// export * as remark from './remarkWrapper' <===!!!!!!
export { default as getLang} from './getLang'
```

After doing so, the error described in this article no longer occurred.

---

Below is the original post

---

I will provide a description of the situation and a solution for the following error.

This error is likely to occur, especially when using barrel exports.

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/552712/8160586e-82e9-daa0-5c8b-5e72d0e37a37.png)

## Situation

- Next.js: 13.5.6
- Modules are imported into the `Client Component`.
- These modules are exported from the `index.ts` file.

```tsx
// ButtonTest.tsx
'use client' <=== Client Component Only!!!!

import { testHello } from '@/lib/testHello' <=== OK (/lib/testHello.ts)
import * as TEST from '@/lib'               <=== NG (/lib/index.ts)
import {testHello} from '@/lib'             <=== NG (/lib/index.ts)
import {testHello} from '@/lib/index'       <=== NG (/lib/index.ts)
import {testHello} from '@/lib/libs'        <=== OK (/lib/libs.ts)
import * as TEST from '@/lib/index.lib'     <=== OK (/lib/index.lib.ts)


export const ButtonTest = () => {
  return (
    <div>
      <button
        onClick={() => {
          testHello()
        }}
      >
        ButtonTest
      </button>
    </div>
  )
}

```

## Solution

Please use a file name other than `index.ts` for exporting. Any other file name, such as `index.lib.ts` or `lib.ts`, will work.

## Supplementary Information

I haven't tested the 'index.js' pattern, but it probably won't work.

Is there no mechanism to resolve imports using the 'index' file on the client side?

If you have any other solutions besides the one mentioned above, please let me know.