otto
otto

Reputation: 2033

How to prevent rollup from looking in parent node_modules folder

My Folder structure is the following:

/dev
  node_modules
  /lib
    node_modules
    rollup.config.js

and this my rollup.config.js

import babel from '@rollup/plugin-babel'
import image from '@rollup/plugin-image'
import json from '@rollup/plugin-json'
import commonjs from 'rollup-plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import sourcemaps from 'rollup-plugin-sourcemaps'
import postcss from 'rollup-plugin-postcss'
import peerDepsExternal from 'rollup-plugin-peer-deps-external'
import del from 'rollup-plugin-delete'

export default {
    input: 'src/index.jsx',
    output: {
        dir: `dist`,
        format: 'es',
        exports: 'auto',
        sourcemap: true,
    },
    // inlineDynamicImports: true, //creates single output file but then there is no intellisense
    plugins: [
        del({ targets: 'dist/*' }),
        // replace({
        //     'process.env.NODE_ENV': JSON.stringify('development'),
        // }),
        peerDepsExternal(),
        nodeResolve({
            // modulesOnly: true, // crashes everything
            extensions: ['.js', '.jsx'],
        }),
        postcss({
            config: {
                path: './postcss.config.js',
            },
            extensions: ['.css', '.scss'],
            minimize: true,
            use: [
                [
                    'sass',
                    {
                        includePaths: ['./src/scss', './node_modules'],
                    },
                ],
            ],
        }),

        json(),
        image(),

        babel({
            exclude: 'node_modules/**',
            plugins: ['@babel/transform-runtime'],
            babelHelpers: 'runtime',
        }),
        commonjs({
            include: 'node_modules/**',
        }),
        sourcemaps(),
    ],
}

I build the code with rollup -c When the node_modules in /dev are installed the I get this error when I want to build the code: Error: 'default' is not exported by ../node_modules/prop-types/index.js, imported by ../node_modules/react-redux/es/components/Provider.js When I delete the /dev/node_modules folder and the build it again then everything works fine. How can I prevent rollup from looking in parent node_modules folder?

Upvotes: 1

Views: 2078

Answers (2)

CodeBloodedChris
CodeBloodedChris

Reputation: 246

The issue is not roll-up itself, but rather the plugin @rollup/plugin-node-resolve. The plugin attempts to resolve modules using the Node resolution algorithm, for using third party modules and that algo iterates through the parent directories, appending each one with "/node_modules" looking for a resolution. https://nodejs.org/api/modules.html#modules_all_together So the issue lies in having a monorepo and using this plugin. It's doesn't seem possible to stop this plugin behavior, however you can stop it from traversing specific module folders.

nodeResolve({
        resolveOnly: [/^(?!react$|react-dom$).*/]
    }),

Per the docs: https://github.com/rollup/rollup-plugin-node-resolve By using the resolveOnly option you can pass in regex expressions to block it from resolving specific modules, or pass in an array of strings specifying the only modules you want it to traverse. The above example does not traverse the react or react-dom modules in the parent folder.

Upvotes: 3

Borni.Mr
Borni.Mr

Reputation: 679

did you try to remove the include in commonJs

 commonjs(),
 sourcemaps(),

Upvotes: -1

Related Questions