汇总错误:node_modules/react/index.js 未导出“默认”

Posted

技术标签:

【中文标题】汇总错误:node_modules/react/index.js 未导出“默认”【英文标题】:Rollup Error: 'default' is not exported by node_modules/react/index.js 【发布时间】:2020-07-28 22:09:29 【问题描述】:

我有一个项目,我使用 webpack 并想切换到 rollup.js,但我在插件 @rollup/plugin-commonjs 方面遇到了麻烦。

我的 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;

我遇到的错误不知道如何解决

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)

https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module 页面并没有真正的帮助,因为我已经在我的配置中使用了所有已命名的导出。

【问题讨论】:

【参考方案1】:

我终于找到了问题所在。我需要@rollup/plugin-replaceplugin 来替换process.env.NODE_ENV

这是工作代码

它还需要一些命名的Exports。

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;

【讨论】:

以上是关于汇总错误:node_modules/react/index.js 未导出“默认”的主要内容,如果未能解决你的问题,请参考以下文章

Linq学习(零)-错误汇总

网页错误页提示码汇总及使用示例

react 报红错误汇总

Linux错误汇总

KVM使用错误汇总

SSIS错误汇总