Skip to content

Instantly share code, notes, and snippets.

@ioxua
Created January 25, 2024 17:46
Show Gist options
  • Save ioxua/8f23b6419b5ce55dba82e6b81fe6dacb to your computer and use it in GitHub Desktop.
Save ioxua/8f23b6419b5ce55dba82e6b81fe6dacb to your computer and use it in GitHub Desktop.
Referencing node_modules files on a JS monorepo using NPM Workspaces

Consider the following JS monorepo folder structure:

.
├── apps
│   ├── backend
│   └── react-app
│       ├── node_modules      <-- INNER NODE_MODULES
│       ├── package-lock.json
│       └── package.json
├── node_modules              <-- OUTER NODE_MODULES
├── package-lock.json
└── package.json

When installing dependencies for apps/react-app, some files may be saved in ./node_modules instead of apps/react-app/node_modules.

This behavior may break any kind of file references inside inner node_modules folders, since you cannot assume where the dependency will be installed.

I could not find out whether this behavior is configurable at all, contributions are welcome

Example: Tailwind CSS + React + Flowbite

This issue may be encountered when changing tailwind.config.ts, since you have to add flowbite-react to the Tailwind content array, as seen below:

import type { Config } from "tailwindcss";
import flowbite from 'flowbite/plugin';
export default {
content: [
"./app/**/*.{js,jsx,ts,tsx}",
// This reference may not work, since there is no guarantee `flowbite-react` is located here
"./node_modules/flowbite-react/**/*.js",
// Adding a relative path to the outer node_modules solves the issue
"../../node_modules/flowbite-react/**/*.js",
],
theme: {
extend: {},
},
plugins: [flowbite],
} satisfies Config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment