Reputation:
I'm trying to set up React with typescript, i followed the tutorial here. I installed react-select
but when i try to reference it i get a compiler error Build: Cannot find module 'react-select'
, i get the same error if i try to run webpack from the cmd line.
I tried including the following loader as suggested as a fix on github but I get the same error.
{
test: /\.js$/,
include: path.join(__dirname, 'node_modules', 'react-select'),
loader: 'jsx-loader',
}
Any ideas?
UPDATE:
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"
},
};
Upvotes: 5
Views: 11882
Reputation: 879
I fixed it by pointing into the dist file:
import CreatableSelect from "react-select/creatable/dist/react-select-creatable.cjs";
Upvotes: 0
Reputation: 81
I was looking at my packges and I found that react-select
package is missing from @types
in node_modules
, so I ran this command # npm install --save @types/react-select
and it solved it.
Upvotes: 5
Reputation: 5646
First of all you need to install the typings for react-select
in order to import it. Once you do that go to the installed typings and check the type of export being done.
In case it is something like export = Select
you need to do import = require('react-select')
In case it is something like export default Select
you need to do import Select from
react-select`
In case of named exports i.e export {Select}
you need to do import {Select} from 'react-select'
In case of multiple named exports you will have to explicitly import each export or do import * as Select from 'react-select'
According to the typings for react-select
as shown here the module exports its contents through a default export at the bottom of the file. So import ReactSelect from 'react-select'
should work for you
Upvotes: 3
Reputation: 2235
Configuring Typsecript with webpack
should be defined something like that:
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'
};
The ts-loader
loads the enables the Typescript to the browser and the source-map-loader
loads the source-maps to the browser for easier debugging.
Let me know if you need anything or if I misunderstood you.
Upvotes: 0