Next.js Discord

Discord Forum

monorepo setup with shared code without an npm package (externalDir + transpile modules)

Unanswered
Serbian Hound posted this in #help-forum
Open in Discord
Serbian HoundOP
NextJS v. 13.4.9

Hi,

I have the following project structure
/packages/projectone/frontend => NextJS project
/packages/projecttwo/frontend => NextJS project

And I want to have a new folder
/packages/shared/components => Folder with .tsx files, without a package.json
For example
/packages/shared/components/SharedBanner.tsx => React functional component that uses chakra ui, so there's an import { Box, Container, Stack, Text } from '@chakra-ui/react' in it and the actual code returning the component

I thought this would be possible by using NextJS's experimental externalDir + typescript's type alias + webpack's config.resolve.alias set to the components folder, but it does not work and throws

[NEXT] - error ../../shared/components/SharedBanner.tsx
[NEXT] Module not found: Can't resolve 'react/jsx-dev-runtime'

Also I had to create a package.json and add peerDependencies to chakra-ui/react and react in order for NextJS not to complain that the chakra-ui/react module does not exist.

So did I misunderstand something and is this just not possible? And is there another easier or better way to share code between two NextJS projects without having to create a package and npm link it or publish it?

0 Replies