I want to know more about monorepo projects. I have a repository named dnd which is using Yarn workspaces with Lerna. The repository contains two main directories.
packages/core
packages/app
The core directory contains utility methods and the app directory is using create-react-app boilerplate. Now here is the thing I want to use my utility methods in the main app components something like this.
import { concat } from '@dnd/core';
Currently, I am importing something like this
import { concat } from '../../../core/lib/utils';
I have to traverse the relative path with this ugly syntax. Now the path is correct but create-react-app throwing an error.
Module not found: You attempted to import ../../../core/lib/utils which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
Is there a way we can simply import our core directory methods inside our app components with something like this.
import { concat } from '@dnd/core';
Repositry: Link
dnd/package.json:
{
"name": "root",
"private": true,
"scripts": {
"bootstrap": "lerna bootstrap --use-workspaces"
},
"devDependencies": {
"lerna": "^3.21.0"
},
"workspaces": [
"packages/*"
]
}
dnd/lerna.json:
{
"useWorkspaces": true,
"packages": [
"packages/*"
],
"version": "0.0.0"
}
dnd/packages/core/package.json:
{
"name": "@dnd/core",
"version": "0.0.0",
"description": "> TODO: description",
"homepage": "",
"license": "ISC",
"main": "lib/core.js",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
}
}
dnd/packages/core/lib/utils.js:
export const concat = (...args) => {
return ''.concat(...args);
};
dnd/packages/app/package.json:
{
"name": "@dnd/rain",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
dnd/packages/app/src/components/App.js:
import React from 'react';
import { concat } from '../../../core/lib/utils';
const App = () => {
return (
<div className="App">
{ concat('Hello', 'World', 'Culture') }
</div>
);
};
export default App;