32

I have a project where I use webpack and want to switch to rollup.js but I have trouble regarding the plugin @rollup/plugin-commonjs.

My rollup.conf.js

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
import nodePolyfills from 'rollup-plugin-node-polyfills';

const config = {
    input: 'site/templates/scripts/master.js',
    output: [
        {
            file: 'site/templates/scripts/master.min.js',
            format: 'cjs'
        }
    ],
    plugins: [
        nodePolyfills(),
        resolve({
            browser: true
        }),
        commonjs({
            include: /node_modules/,
            namedExports: {
                'react': ["useState", "useEffect"],
                '@apollo/client': ['ApolloProvider', 'ApolloClient', 'HttpLink', 'InMemoryCache', 'useQuery', 'gql'],
                'styled-components': [ 'styled', 'css', 'ThemeProvider' ]
            }
        }),
        babel({
            babelrc: true,
            exclude: 'node_modules/**'
        }),
        terser()
    ]
};

export default config;

The Error I'm getting and don't know to solve

site/templates/scripts/master.js → site/templates/scripts/master.min.js...
[!] Error: 'default' is not exported by node_modules/react/index.js, imported by site/templates/scripts/src/BgProductRecommendations/FredhopperProduct.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
site/templates/scripts/src/BgProductRecommendations/FredhopperProduct.js (3:7)
1: 'use strict';
2: 
3: import React from "react";
          ^
4: 
5: const FredhopperProduct = ({
Error: 'default' is not exported by node_modules/react/index.js, imported by site/templates/scripts/src/BgProductRecommendations/FredhopperProduct.js
    at error (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:10152:30)
    at Module.error (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:14487:16)
    at handleMissingExport (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:14388:28)
    at Module.traceVariable (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:14871:24)
    at ModuleScope.findVariable (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:13448:39)
    at FunctionScope.findVariable (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:8661:38)
    at ChildScope.findVariable (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:8661:38)
    at MemberExpression.bind (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:11285:49)
    at CallExpression$1.bind (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:8746:23)
    at CallExpression$1.bind (/usr/local/lib/node_modules/rollup/dist/shared/rollup.js:11473:15)

The https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module page does not really help, since I have all named exports which I use in my config.

4 Answers 4

19

Finally I found what was wrong. I needed the @rollup/plugin-replaceplugin to replace process.env.NODE_ENV

Here's the working code

It needed also some more namedExports.

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
import nodePolyfills from 'rollup-plugin-node-polyfills';
import replace from '@rollup/plugin-replace';

import React from 'react';
import ReactIs from 'react-is';
import ReactDOM from 'react-dom';

const config = {
    input: 'site/templates/scripts/master.js',
    output: [
        {
            file: 'site/templates/scripts/master.min.js',
            format: 'cjs'
        }
    ],
    plugins: [
        replace({
            "process.env.NODE_ENV": JSON.stringify("development")
        }),
        nodePolyfills(),
        resolve({
            browser: true
        }),
        commonjs({
            include: /node_modules/,
            namedExports: {
                'react-is': Object.keys(ReactIs),
                'react': Object.keys(React),
                'react-dom': Object.keys(ReactDOM),
                '@apollo/client': ['ApolloProvider', 'ApolloClient', 'HttpLink', 'InMemoryCache', 'useQuery', 'gql'],
                'styled-components': [ 'styled', 'css', 'ThemeProvider' ]
            }
        }),
        babel({
            babelrc: true,
            exclude: 'node_modules/**'
        }),
        terser()
    ]
};

export default config;
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm. I must be doing something wrong because I have all of the identified changes and still get that error.
@MikeS. Check your packages.json file and add react at least for peerDependencies not only in devDependencies. This may help.
You're a life-saver--this combination of plugins fixed my issue!
10

The solution for this problem is to update commonjs option to make it as below

import commonjs from '@rollup/plugin-commonjs';
// rollup.config.js
export default {
  input: 'src/app.ts',
  output: [
    {
...
    commonjs({
      include: /node_modules/,
      requireReturnsDefault: 'auto', // <---- this solves default issue
    }),

documentation link: https://www.npmjs.com/package/@rollup/plugin-commonjs search for requireReturnsDefault

2 Comments

For me the issue was that I needed it to work for external packages as well (e.g. lodash), so I had to switch to requireReturnsDefault: 'esmExternals'. But your answer got me to that setting, so thanks!
how about with esm, I do not want to output cjs.
4

As of the latest @rollup/plugin-commonjs version, namedExports are handled by default.

Also try using this rollup babel config, requires the @babel/preset-react module to be installed

     babel({
        exclude: "node_modules/**",
        presets: ["@babel/preset-react"],
        babelHelpers: "bundled",
      }),

And since the date of the issue most rollup plugins have been namespaced so try to reinstall @rollup/plugin-babel

Comments

0

enter image description here

My project based on vite,use @vitejs/plugin-react-swc replace @vitejs/plugin-react works for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.