Typescript / Webpack:找不到模块' react-select'

时间:2016-09-13 09:55:28

标签: reactjs typescript webpack

我尝试使用打字稿设置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"
    },
};

3 个答案:

答案 0 :(得分:2)

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

如果类似于export = Select,则需要执行import = require('react-select')

如果它类似于export default Select,您需要执行import Select from react-select`

如果是指定的导出,export {Select},则需要执行import {Select} from 'react-select'

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

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

答案 1 :(得分:1)

我正在查看我的包装,但我发现react-select中的@types中缺少node_modules个包,因此我运行了此命令# npm install --save @types/react-select并解决了它。< / p>

答案 2 :(得分:0)

使用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将源映射加载到浏览器以便于调试。

如果您需要任何内容​​或者我误解了您,请告诉我。