I'm working on a monorepo project using pnpm, Turborepo, and Vite to manage multiple packages. One of my packages (@myorg/ui) generates a dist/ folder during the build process, but the build includes a node_modules folder inside dist/, which I don’t want to publish to NPM or my private registry (Azure Artifacts).
My Current Setup:
- Build Tool: Vite (with a
vite.config.ts). - Monorepo Tool: Turborepo, to handle dependencies and caching.
- Package Manager: pnpm.
- Publishing Workflow:
- Clean the project(removes previous builds +
dist+node_modules). - Build with
pnpm build(calls Vite to generate thedist/folder). - Publish with Changesets (
changeset publish).
- Clean the project(removes previous builds +
My Problem:
After building, the dist/ folder contains a node_modules subfolder that I don’t want:
dist/
├── _virtual/
├── components/
├── hooks/
├── node_modules/ <-- I want to exclude this
├── styles/
├── index.d.ts
├── index.mjs
└── style.css
This happens despite configuring rollupOptions.external in my vite.config.ts to exclude dependencies like react and react-dom.
My vite.config.ts:
import path from 'node:path';
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
export default defineConfig({
plugins: [
react({ jsxRuntime: 'classic' }),
dts({ insertTypesEntry: true }),
],
build: {
lib: {
entry: path.resolve(__dirname, 'src/index.ts'),
formats: ['es'],
fileName: (format) => `index.${format}.js`,
},
rollupOptions: {
output: {
exports: 'named',
preserveModules: true,
},
external: [
'react',
'react-dom',
'next',
/^next.*/,
/^@myorg\/utils.*/,
],
},
},
});
What I've Tried:
.npmignore: Addingdist/node_modulesto.npmignorepreventsnode_modulesfrom being published but doesn’t remove it from thedist/folder during the build.- Cleaning Scripts: I’ve tried adding custom scripts to clean
dist/node_modulesafter the build, but this feels like a hacky solution. - Vite External Configuration: I ensured
rollupOptions.externalincludes all dependencies, but it doesn’t preventnode_modulesfrom being copied intodist/.
My Question:
How can I configure my build process (Vite or otherwise) to prevent node_modules from being included in the dist/ folder? Is there something I’m missing in my Rollup or Vite configuration?
Additional Details:
- Package Manager: pnpm
- Monorepo Manager: Turborepo
- Tools: Vite, Changesets