【问题标题】:Typescript/Webpack: cannot find module 'react-select'打字稿/Webpack:找不到模块“反应选择”
【发布时间】:2016-09-13 09:55:28
【问题描述】:

我正在尝试使用 typescript 设置 React,我按照教程 here 进行操作。我安装了react-select,但是当我尝试引用它时,我得到一个编译器错误Build: Cannot find module 'react-select',如果我尝试从 cmd 行运行 webpack,我会得到同样的错误。

我尝试按照 github 上的建议包含以下加载程序作为修复,但我得到了同样的错误。

 {
    test: /\.js$/,
    include: path.join(__dirname, 'node_modules', 'react-select'),
    loader: 'jsx-loader',
  }

有什么想法吗?

更新:

tsconfig.json

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "es6",
        "jsx": "react"
    },
    "files": [
        "./typings/index.d.ts",
        "./src/components/Hello.tsx",
        "./src/index.tsx"
    ]
}

package.json:

{
  "name": "react-typescript-setup",
  "version": "1.0.0",
  "main": "./dist/bundle.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "webpack": "webpack -w"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.3.1",
    "react-dom": "^15.3.1",
    "react-select": "^1.0.0-rc.1"
  },
  "devDependencies": {
    "css-loader": "^0.25.0",
    "react-select": "^1.0.0-rc.1",
    "source-map-loader": "^0.1.5",
    "style-loader": "^0.13.1",
    "ts-loader": "^0.8.2",
    "typings": "^1.3.3"
  },
  "description": ""
}

webpack.config.js

var path = require('path');

module.exports = {
    entry: "./src/index.tsx",
    output: {
        path: __dirname,
        filename: "./dist/bundle.js",
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",
    debug: true,

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },

    module: {
        loaders: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
            { test: /\.tsx?$/, loader: "ts-loader" },
            {
                test: /\.css$/,
                loader: 'style!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
            }
        ],

        preLoaders: [
            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { test: /\.js$/, loader: "source-map-loader" }
        ]
    },

    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    },
};

【问题讨论】:

    标签: reactjs typescript webpack


    【解决方案1】:

    首先,您需要安装 react-select 的类型才能导入它。完成后,转到已安装的类型并检查正在完成的导出类型。

    如果是export = Select,你需要做import = require('react-select')

    如果是export default Select,你需要做import Select fromreact-select`

    如果是命名导出,即export {Select},您需要执行import {Select} from 'react-select'

    如果有多个命名导出,您必须显式导入每个导出或执行import * as Select from 'react-select'

    根据react-select 的类型,如here 所示,模块通过文件底部的默认导出导出其内容。所以import ReactSelect from 'react-select' 应该适合你

    【讨论】:

      【解决方案2】:

      我在查看我的包,发现node_modules 中的@types 中缺少react-select 包,所以我运行了这个命令# npm install --save @types/react-select 并解决了它。

      【讨论】:

        【解决方案3】:

        使用webpack 配置 Typsecript 应该这样定义:

        module.exports = {
          entry: "./app/boot",
          output: {
            path: __dirname,
            filename: "./dist/bundle.js"
          },
          resolve: {
            extensions: ['', '.js', '.ts']
          },
          module: {
            loaders: [
              { test: /\.ts/,   loader: ["ts-loader"], exclude: /node_modules/ },
            ],
            preLoaders: [
              // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
              { test: /\.js$/, loader: "source-map-loader", exclude: ['node_modules', 'ext-*', 'lib', 'tools'] }
            ]
          },
          debug: true,
          devtool: 'source-map'
        };
        

        ts-loader 将 Typescript 加载到浏览器,source-map-loader 将源映射加载到浏览器以便于调试。

        如果你需要什么或者我误解了你,请告诉我。

        【讨论】:

        • 嗯,这就是我所拥有的,我刚刚安装了 react-select 的类型,这改变了错误,现在它说它不能使用这个结构导入..
        • 什么是>这个结构?
        • 任何 ES6 导入语句,Import * as Select,Import Select from,import { Select } from ..
        • 但是如果我使用 require.. `import Select = require("react-select");`
        • 您的环境是否配置为使用 es6 语法?
        猜你喜欢
        • 2017-04-15
        • 1970-01-01
        • 2021-09-30
        • 2023-03-06
        • 2020-11-22
        • 1970-01-01
        • 2021-01-07
        • 2013-12-30
        相关资源
        最近更新 更多