Skip to content

Why does the preserveModules option generate node_modules folder #3684

@ghbakhtiari

Description

@ghbakhtiari

Documentation Is:

  • Missing
  • Needed
  • Confusing
  • Not Sure?

As described here in the docs, I'm using the plugin-node-resolve plugin to include the dependencies of my library in the final bundle.

I'm also using the external option as described here to exclude the peerDependencies. So the final rollup config looks something like this:

import path from 'path';
import resolve from '@rollup/plugin-node-resolve';
import babel from '@rollup/plugin-babel';

export default {
  input: 'src/index.js',
  output: {
    dir: './dist',
    format: 'es',
    preserveModules: true,
  },
  external: [
    'prop-types',
    'react',
    'react-dom',
  ],
  plugins: [
    resolve({
      extensions: ['.js', '.jsx', '.json'],
    }),
    babel({
      babelHelpers: 'bundled',
      exclude: 'node_modules/**',
    }),
  ],
};

The problem is that after the build, the dependencies of the package are generated in this location: dist/node_modules/PACKAGE_NAME. And their import statements in other files are something like this:
import SOMETHING from '../../../node_modules/PACKAGE_NAME/index.js';

And on the other hand, when I build the package on the consumer project (using prepare script), this nested node_modules directory doesn't exist (possibly because npm and yarn flatten the node_modules structure).

So I was wondering is there any option to fix this problem? e.g. a way to change the name of these generated node_modules folders and their import statements to something else...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions