I'm trying to put together a very basic project with React, TypeScript and Webpack. When I compile I get the following errors from the react
folder in node_modules
(I've removed the stack traces and my project's path for brevity):
ERROR in ./node_modules/react/cjs/react.production.min.js Module not found: Error: Can't resolve 'fbjs/lib/emptyFunction' in '.../node_modules/react/cjs' ERROR in ./node_modules/react/cjs/react.development.js Module not found: Error: Can't resolve 'fbjs/lib/emptyFunction' in '.../node_modules/react/cjs' ERROR in ./node_modules/react/cjs/react.production.min.js Module not found: Error: Can't resolve 'fbjs/lib/emptyObject' in '.../node_modules/react/cjs' ERROR in ./node_modules/react/cjs/react.development.js Module not found: Error: Can't resolve 'fbjs/lib/emptyObject' in '.../node_modules/react/cjs' ERROR in ./node_modules/react/cjs/react.development.js Module not found: Error: Can't resolve 'fbjs/lib/invariant' in '.../node_modules/react/cjs' ERROR in ./node_modules/react/cjs/react.development.js Module not found: Error: Can't resolve 'fbjs/lib/warning' in '.../node_modules/react/cjs' ERROR in ./node_modules/react/cjs/react.production.min.js Module not found: Error: Can't resolve 'object-assign' in '.../node_modules/react/cjs' ERROR in ./node_modules/react/cjs/react.development.js Module not found: Error: Can't resolve 'object-assign' in '.../node_modules/react/cjs' ERROR in ./node_modules/react/cjs/react.development.js Module not found: Error: Can't resolve 'prop-types/checkPropTypes' in '.../node_modules/react/cjs'
I tried uninstalling TypeScript and replacing it with Babel to transpile the JSX and got the same error. Installing babel-preset-2015
fixed it.
I've tried just about every combination of target
and module
in tsconfig.json
to get the same result in TypeScript but couldn't get anything working. How can I get Webpack, TypeScript, and React working together?
I've worked with all three of these technologies before, is it a recent compatibility problem? If so what are the most recent compatible versions?
I've seen a few other questions similar to this one where the solution was installing fbjs
directly in the project - I've tried this too without success.
{ "compilerOptions": { "target": "es5", "jsx": "react", "module": "es2015" }, "exclude": ["build"] }
module.exports = { entry: { dev: "./src/index.tsx", }, output: { filename: "./build/index.js", }, devtool: "source-map", resolve: { extensions: [".ts", ".tsx"], }, module: { loaders: [ // Typescript { test: /\.tsx?$/, loader: "ts-loader" }, ], }, };
{ ... "scripts": { "build": "webpack" }, "devDependencies": { "@types/react": "^16.0.28", "ts-loader": "^3.2.0", "typescript": "^2.6.2", "webpack": "^3.10.0" }, "dependencies": { "react": "^16.2.0" } }
import * as React from "react"; const a = <div />;
(I'm running this with Node 9.2.1 and NPM 5.6.0 installed)
To solve the "Module not found: Can't resolve" error in React, make sure to install the package from the error message if it's a third-party package, e.g. npm i somePackage . If you got the error when importing local files, correct your import path.
To create a React App with Typescript, run the npx create-react-app command with the --template flag set to typescript , e.g. npx create-react-app my-ts-app --template typescript . Copied! If you get an error, try forcing the command to the latest version of create-react-app.
Webpack is not resolving .js
files. Add this to your webpack.config.js
.
resolve: { extensions: [".ts", ".tsx", ".js", ".jsx"] },
Here is the tsconfig.json
I used to run your example.
{ "compilerOptions": { "jsx": "react", "lib": ["es6", "dom"], "rootDir": "src", "module": "commonjs", "target": "es5", "sourceMap": true, "moduleResolution": "node", "noImplicitReturns": true, "noImplicitThis": true, "noImplicitAny": true, "strictNullChecks": true }, "include": [ "./src" ], "exclude": [ "node_modules", "build" ] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With