gpt4 book ai didi

javascript - node_modules/@types/react/index"' 没有默认导出

转载 作者:搜寻专家 更新时间:2023-10-30 20:34:25 25 4
gpt4 key购买 nike

尝试将 React 应用程序转换为 Typescript 并遇到奇怪的错误。

node_modules/@types/react/index"' has no default export.

node_modules/@types/react-dom/index"' has no default export.

我为 typescript 设置了 tsconfig 和 webpack。将此组件的扩展名从 .js 更改为 .tsx 后,我收到 React 错误?

enter image description here

想法?

tsconfig.json

{
"compilerOptions": {
"outDir": "./moonholdings/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react"
},
"include": [
"./app/**/*"
]
}

网页包

/* eslint-disable no-console */
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import path from 'path';
import chalk from 'chalk';

const moonholdings = path.resolve(__dirname, 'moonholdings');
const app = path.resolve(__dirname, 'app');
const nodeModules = path.resolve(__dirname, 'node_modules');

const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: path.join(__dirname, '/app/index.html'),
inject: 'body'
});

const ExtractTextPluginConfig = new ExtractTextPlugin({
filename: 'moonholdings.css',
disable: false,
allChunks: true
});

const CopyWebpackPluginConfigOptions = [{
from: 'app/static',
to: 'static/'
}];

const CopyWebpackPluginConfig = new CopyWebpackPlugin(CopyWebpackPluginConfigOptions);

const PATHS = {
app,
build: moonholdings
};

const LAUNCH_COMMAND = process.env.npm_lifecycle_event;

const isProduction = LAUNCH_COMMAND === 'production';
process.env.BABEL_ENV = LAUNCH_COMMAND;

const productionPlugin = new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
});

const base = {
// entry: ['babel-polyfill', PATHS.app],
entry: './app/index.tsx',
output: {
path: PATHS.build,
publicPath: '/',
filename: 'index_bundle.js'
},
resolve: {
modules: [app, nodeModules],
extensions: ['.ts', '.tsx', '.js', '.json']
},
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.tsx?$/, loader: 'awesome-typescript-loader' },
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.s?css/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)/,
loader: 'file-loader?name=[path][name].[ext]'
},
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader' }
]
}
};

const developmentConfig = {
devtool: 'cheap-module-inline-source-map',
devServer: {
contentBase: moonholdings
},
plugins: [
CopyWebpackPluginConfig,
ExtractTextPluginConfig,
HtmlWebpackPluginConfig
]
};

const productionConfig = {
devtool: 'cheap-module-source-map',
plugins: [
CopyWebpackPluginConfig,
ExtractTextPluginConfig,
HtmlWebpackPluginConfig,
productionPlugin
]
};

console.log(`${chalk.magenta('฿')} ${chalk.green('yarn run:')} ${chalk.red(LAUNCH_COMMAND)}`);

export default Object.assign(
{}, base,
isProduction === true ? productionConfig : developmentConfig
);

最佳答案

您必须使用 import * as React from "react"; 而不是 import React from 'react'

发生这种情况是因为 babel(您之前使用的那个)假定 modules.export 作为默认导出,而 typescript(您现在使用的那个)没有。

关于javascript - node_modules/@types/react/index"' 没有默认导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51902844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com