【问题标题】:node_modules/@types/react/index"' has no default exportnode_modules/@types/react/index"' 没有默认导出
【发布时间】:2019-01-24 22:28:32
【问题描述】:

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

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

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

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

想法?

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
);

【问题讨论】:

    标签: javascript reactjs typescript


    【解决方案1】:

    我在使用Next.js 时遇到了类似的问题,发现在这种情况下可以简单地省略import 声明。像下面这样的组件将删除第一行。

    import React from "react"; // Delete this line
    
    function MyComponent(): JSX.Element {
      return (
        <div>MyComponent</div>
      );
    }
    
    export default MyComponent;
    

    如果需要,仍然可以从 React 导入组件、钩子等,例如:

    import { useState } from "react";
    

    请注意,我已经将GoalsAndGambles' answer 中描述的选项添加到我的tsconfig.json 文件中。

    【讨论】:

      【解决方案2】:

      您只需将"allowSyntheticDefaultImports": true"esModuleInterop":true 添加到您的tsconfig.json 即可使用此语法

      {
        ...
        "compilerOptions": {
          "allowSyntheticDefaultImports": true,
          "esModuleInterop": true,
        },
        ...
      }
      

      【讨论】:

      • 非常好!!这也将解决“没有默认导出”的所有问题。
      • 根据这个答案 (stackoverflow.com/a/54674521/416714) 从 TypeScript 2.7 开始,您需要添加的选项是 "esModuleInterop": true
      • 这应该是最佳答案。这解决了当您想要导入类型和默认类型时的问题 - 您无法使用 * as React 方法。
      • "esModuleInterop": true 选项就足够了。
      • 这应该是最好的答案。我看到很多人建议修改 import 语句,我认为这是错误的举动。
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-12-05
        • 1970-01-01
        • 2020-11-28
        • 1970-01-01
        • 2020-07-28
        • 2021-03-10
        • 2022-08-02
        • 2021-01-25
        • 2021-05-22
        相关资源
        最近更新 更多