In my monorepo setup with nextjs, lerna, and npm workspaces, the folder structure is as follows:
packages
next-js-app
pages
index.tsx
tsconfig.json
ui-library
src
components
dropdown.tsx
index.ts
utils.ts
tsconfig.json
package.json
lerna.json
tsconfig.json
I am trying to import the ui-library
into the next-js-app
like this:
// packages/next-js-app/pages/index.tsx
import { UiLibrary } from '@workspaceName/ui-library'
To achieve this, I added externalDir: true
under the experimental
key in the next.config.js
within the next-js-app
:
module.exports = {
...
experimental: {
externalDir: true
}
...
}
Issue
The import works fine, but in the file
packages/ui-library/src/components/dropdown.tsx
, there is a line:
// packages/ui-library/src/components/dropdown.tsx
import { helperFunction } from 'utils'
This means I am trying to import helperFunction
from packages/ui-library/src/utils.ts
.
When I run the next dev
script from packages/next-js-app
, I encounter the following error:
wait - compiling...
error - ../ui-library/src/components/dropdown.tsx:3:0
Module not found: Can't resolve 'utils'
1 | ...
2 | ...
> 3 | import { helperFunction } from "utils"
4 |
5 | const Dropdown = () => {
6 | const onClick = (event) => {
However, running the ui-library
separately allows the use of absolute paths in imports without any issues.
Query
How can I resolve the issue of absolute imports in this scenario?