Module build failed (from ./node_modules/ts-loader/index.js): Error: TypeScript emitted no output for - reactjs

**Error**
**ERROR in ./src/app-chat.tsx
Module build failed (from ./node_modules/ts-loader/index.js):
Error: TypeScript emitted no output for D:\React\kasper\new-kasper\kasper-ui-chat\chat\src\app-chat.tsx.
at makeSourceMapAndFinish (D:\React\kasper\new-kasper\kasper-ui-chat\chat\node_modules\ts-loader\dist\index.js:52:18)
at successLoader (D:\React\kasper\new-kasper\kasper-ui-chat\chat\node_modules\ts-loader\dist\index.js:39:5)
at Object.loader (D:\React\kasper\new-kasper\kasper-ui-chat\chat\node_modules\ts-loader\dist\index.js:22:5)
webpack 5.74.0 compiled with 1 error in 7299 ms**
Webpack
const { merge } = require('webpack-merge');
const Dotenv = require('dotenv-webpack');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const FilterWarningsPlugin = require('webpack-filter-warnings-plugin');
const path = require('path');
const singleSpaDefaults = require('webpack-config-single-spa-react');
const override = require('./config-overrides.js');
module.exports = (webpackConfigEnv, argv) => {
const defaultConfig = singleSpaDefaults({
orgName: 'app',
projectName: 'chat',
webpackConfigEnv,
argv,
override,
});
return merge(defaultConfig, {
resolve: {
extensions: ['.ts', '.tsx', '.js'],
preferRelative: true,
alias: {
containers: path.resolve(__dirname, 'src/containers'),
components: path.resolve(__dirname, 'src/components'),
layouts: path.resolve(__dirname, 'src/layouts'),
stores: path.resolve(__dirname, 'src/stores'),
hooks: path.join(__dirname, 'src/hooks'),
context: path.join(__dirname, 'src/context'),
assets: path.join(__dirname, 'src/assets'),
helpers: path.join(__dirname, 'src/helpers'),
},
// plugins: [new TsconfigPathsPlugin()],
},
devServer: {
port: 8500,
},
plugins: [
new Dotenv({
path: './.env.development',
}),
new FilterWarningsPlugin({
exclude:
/There are multiple modules with names that only differ in casing/,
}),
],
output: {
path: path.resolve(__dirname, 'build'),
assetModuleFilename: 'assets/[name][ext]',
},
module: {
rules: [
{
test: /\.json$/,
use: 'json-loader',
},
{
test: /\.(js)x?$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.(ts)x?$/,
exclude: /node_modules/,
use: 'ts-loader',
},
],
},
});
};
tsconfig.json
{
"extends": "ts-config-single-spa",
"compilerOptions": {
"jsx": "react-jsx",
"declarationDir": "build",
"baseUrl": "./src",
},
"files": ["src/app-chat.tsx"],
"include": ["src/**/*"],
"exclude": ["src/**/*.test*"],
}
.babel
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [["#babel/plugin-proposal-decorators", { "legacy": true }], "inline-react-svg" ]
}
package.json
{
"name": "#app/login",
"scripts": {
"start": "webpack serve",
"start:standalone": "webpack serve --env standalone",
"build": "concurrently yarn:build:*",
"build:webpack": "webpack --mode=production",
"analyze": "webpack --mode=production --env analyze",
"lint": "eslint src --ext js,ts,tsx",
"lint:fix": "eslint src --ext js,ts,tsx --fix",
"format": "prettier --write .",
"check-format": "prettier --check .",
"test": "cross-env BABEL_ENV=test jest",
"watch-tests": "cross-env BABEL_ENV=test jest --watch",
"prepare": "cd .. && husky install",
"coverage": "cross-env BABEL_ENV=test jest --coverage",
"build:types": "tsc"
},
"devDependencies": {
"#babel/core": "^7.15.0",
"#babel/eslint-parser": "^7.15.0",
"#babel/plugin-transform-runtime": "^7.15.0",
"#babel/preset-env": "^7.15.0",
"#babel/preset-react": "^7.14.5",
"#babel/preset-typescript": "^7.15.0",
"#babel/runtime": "^7.15.3",
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^12.0.0",
"#types/js-cookie": "^3.0.2",
"#types/testing-library__jest-dom": "^5.14.1",
"babel-jest": "^27.0.6",
"babel-loader": "^8.2.5",
"concurrently": "^6.2.1",
"cross-env": "^7.0.3",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-ts-react-important-stuff": "^3.0.0",
"eslint-plugin-prettier": "^3.4.1",
"husky": "^7.0.2",
"identity-obj-proxy": "^3.0.0",
"jest": "^27.0.6",
"jest-cli": "^27.0.6",
"json-loader": "^0.5.7",
"prettier": "^2.3.2",
"pretty-quick": "^3.1.1",
"ts-config-single-spa": "^3.0.0",
"ts-loader": "^9.4.1",
"tsconfig-paths-webpack-plugin": "^4.0.0",
"typescript": "^4.3.5",
"webpack": "^5.51.1",
"webpack-cli": "^4.8.0",
"webpack-config-single-spa-react": "^4.0.0",
"webpack-config-single-spa-react-ts": "^4.0.0",
"webpack-config-single-spa-ts": "^4.0.0",
"webpack-dev-server": "^4.0.0",
"webpack-merge": "^5.8.0"
},
"dependencies": {
"#date-io/moment": "^2.13.1",
"#emotion/react": "^11.10.4",
"#emotion/styled": "^11.10.4",
"#material-ui/core": "^4.12.4",
"#material-ui/icons": "^4.11.3",
"#material-ui/pickers": "^3.3.10",
"#mui/material": "^5.10.8",
"#sentry/react": "^6.19.7",
"#sentry/tracing": "^6.19.7",
"#types/jest": "^27.0.1",
"#types/react": "^17.0.19",
"#types/react-dom": "^17.0.9",
"#types/systemjs": "^6.1.1",
"#types/webpack-env": "^1.16.2",
"awesome-phonenumber": "^2.72.0",
"aws-amplify": "^3.3.2",
"aws-amplify-react": "^5.1.9",
"bootstrap": "^5.1.3",
"dotenv-webpack": "^8.0.1",
"eslint-plugin-jest": "^27.1.0",
"eslint-plugin-jest-dom": "^4.0.2",
"eslint-plugin-react": "^7.31.8",
"eslint-plugin-storybook": "^0.6.4",
"eslint-plugin-testing-library": "^5.7.2",
"firebase": "^8.2.2",
"formik": "^2.2.9",
"formik-material-ui": "^3.0.1",
"formik-material-ui-lab": "0.0.8",
"formik-material-ui-pickers": "0.0.12",
"js-cookie": "^3.0.1",
"logrocket": "^3.0.0",
"logrocket-react": "^5.0.1",
"mobx": "^5.15.6",
"mobx-devtools-mst": "^0.9.22",
"mobx-react": "^6.3.1",
"mobx-react-devtools": "^6.1.1",
"mobx-react-router": "^4.1.0",
"mobx-state-tree": "^3.17.2",
"mobx-utils": "^5.6.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-helmet": "^6.1.0",
"react-password-strength-bar": "^0.4.0",
"react-query": "^3.34.16",
"react-router-dom": "^5.2.0",
"single-spa": "^5.9.3",
"single-spa-react": "^4.3.1",
"yup": "^0.32.11",
"yup-phone": "1.3.1"
},
"types": "dist/app-login.d.ts",
"resolutions": {
"#types/react": "^17.0.2"
}
}

In webpack add :
module: {
rules: [
{
test: /\.json$/,
use: 'json-loader',
},
{
test: /\.(js)x?$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.(ts)x?$/,
exclude: /node_modules|\.d\.ts$/, // this line as well
use: {
loader: 'ts-loader',
options: {
compilerOptions: {
noEmit: false, // this option will solve the issue
},
},
},
},
],
},
Usually webpack hangs when declaration is true without creating d.ts files and that causes errors. By default is true so all you need it to set it to false.

Related

Why is css-loader bloating my entry point bundle?

I'm working on cleaning up an older React project and trying to cut down on bundle size by implementing code splitting and chunking things out. I've made considerable progress, but my main entry point for the application is still sitting at ~600kb. It seems to be 95% coming from not the application code itself but the css-loader library I'm using during the webpack build process.
This seems incorrect, but I can't figure out what about my webpack config or packages is causing this bloat in this particular bundle.
Here's my environment-common and production webpack config info:
// webpack.common.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const BUILD_DIR = path.resolve(__dirname, 'build');
const SRC_DIR = path.resolve(__dirname, 'src');
module.exports = {
entry: ['babel-polyfill', `${SRC_DIR}/index.js`],
output: {
path: BUILD_DIR,
publicPath: '/',
filename: '[name].[fullhash].bundle.js',
chunkFilename: '[name].[chunkhash].bundle.js'
},
optimization: {
moduleIds: 'named',
splitChunks: {
chunks: 'all'
}
},
module: {
// exclude node_modules
rules: [
{
test: /\.(js)$/,
exclude: /node_modules/,
use: ['babel-loader']
},
{
test: /\.(scss|css)$/,
use: [
process.env.NODE_ENV !== 'production'
? 'style-loader'
: MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}
]
},
resolve: {
alias: {
'~': path.resolve(__dirname, 'src')
},
extensions: ['*', '.js']
},
plugins: [
new HtmlWebpackPlugin({
inject: true,
template: './public/index.html'
}),
new CopyWebpackPlugin({
patterns: [
{ from: './public/img', to: 'img' },
{ from: './web.config', to: 'web.config' }
]
})
]
};
// webpack.prod.js
const { merge } = require('webpack-merge');
const webpack = require('webpack');
const CompressionPlugin = require('compression-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const common = require('./webpack.common');
const config = require('./config/config.prod.json');
const extractCSS = new MiniCssExtractPlugin({ filename: '[name].fonts.css' });
const extractSCSS = new MiniCssExtractPlugin({ filename: '[name].styles.css' });
process.traceDeprecation = true;
module.exports = merge(common, {
mode: 'production',
devtool: 'source-map',
plugins: [
new webpack.DefinePlugin({
API_BASE_URL: JSON.stringify(config.API_BASE_URL),
TUMBLR_CLIENT_BASE_URL: JSON.stringify(config.TUMBLR_CLIENT_BASE_URL)
}),
extractCSS,
extractSCSS,
new CompressionPlugin()
],
optimization: {
splitChunks: {
chunks: 'all'
},
minimize: true
}
});
// package.json
{
"name": "***",
"version": "1.0.0",
"description": "***",
"author": "***",
"url": "***",
"copyright": "***",
"license": "GPL",
"private": true,
"homepage": "***",
"devDependencies": {
"#babel/cli": "^7.1.5",
"#babel/core": "^7.1.6",
"#babel/eslint-parser": "^7.13.8",
"#babel/plugin-proposal-nullish-coalescing-operator": "^7.13.8",
"#babel/plugin-proposal-object-rest-spread": "^7.0.0",
"#babel/plugin-proposal-optional-chaining": "^7.13.8",
"#babel/plugin-transform-runtime": "^7.4.0",
"#babel/preset-env": "^7.1.6",
"#babel/preset-react": "^7.0.0",
"#testing-library/jest-dom": "^5.11.9",
"#testing-library/react": "^11.2.3",
"#testing-library/user-event": "^12.6.2",
"babel-core": "^7.0.0-bridge.0",
"babel-loader": "^9.1.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"codecov": "^3.1.0",
"compression-webpack-plugin": "^10.0.0",
"copy-webpack-plugin": "^11.0.0",
"cross-env": "^5.2.0",
"css-loader": "^6.7.2",
"eslint": "^8.28.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.1.0",
"eslint-import-resolver-webpack": "^0.13.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest-dom": "^3.9.2",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-testing-library": "^5.9.1",
"eslint-watch": "^8.0.0",
"html-webpack-plugin": "^5.5.0",
"jest": "^26.6.3",
"jest-dom": "^4.0.0",
"jest-when": "^2.3.1",
"mini-css-extract-plugin": "^2.7.0",
"mkdirp": "^0.5.1",
"msw": "^0.35.0",
"node-sass": "^8.0.0",
"prettier": "^2.0.2",
"redux-saga-test-plan": "^3.7.0",
"redux-test-utils": "^0.3.0",
"rimraf": "^2.6.2",
"sass-loader": "^13.2.0",
"style-loader": "^3.3.1",
"terser-webpack-plugin": "^5.3.6",
"unused-webpack-plugin": "^2.4.0",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0",
"webpack-dev-server": "^4.11.1",
"webpack-merge": "^5.8.0"
},
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.35",
"#fortawesome/free-regular-svg-icons": "^5.15.3",
"#fortawesome/free-solid-svg-icons": "^5.15.3",
"#fortawesome/react-fontawesome": "^0.1.14",
"availity-reactstrap-validation": "npm:availity-reactstrap-validation-safe#^2.6.1",
"axios": "^0.18.0",
"babel-polyfill": "^6.26.0",
"bootstrap": "^4.1.3",
"chalk": "^2.4.1",
"classnames": "^2.2.6",
"dot-prop-immutable": "^1.5.0",
"history": "^4.7.2",
"immutable": "^4.0.0-rc.12",
"jquery": "^3.5.1",
"local-storage": "^1.4.2",
"lodash": "^4.17.20",
"luxon": "^3.1.1",
"promise": "^8.0.2",
"prop-types": "^15.6.2",
"query-string": "^6.2.0",
"rc-tooltip": "^3.7.3",
"react": "^16.8.6",
"react-autosuggest": "^9.4.3",
"react-dom": "^16.8.6",
"react-ga": "^2.5.6",
"react-multivalue-text-input": "^0.6.2",
"react-query": "^3.26.0",
"react-redux": "^5.1.1",
"react-redux-toastr": "^7.4.3",
"react-router": "^6.2.1",
"react-router-dom": "^5.2.0",
"react-table": "^7.6.3",
"react-toastify": "^7.0.3",
"react-transition-group": "^2.5.0",
"reactstrap": "^6.5.0",
"redux": "^4.0.1",
"redux-logger": "^3.0.6",
"redux-saga": "^0.16.2",
"reselect": "^4.0.0",
"simple-line-icons": "^2.4.1",
"styled-components": "^4.1.2",
"uuid": "^8.3.2"
},
"scripts": {
"start": "webpack serve --config webpack.dev.js",
"build": "npm run clean && webpack --config webpack.prod.js",
"build:staging": "npm run clean && webpack --config webpack.staging.js",
"clean": "rimraf ./build",
"lint": "prettier --write \"src/**/*.js\" && eslint src/",
"lint:watch": "esw src/ -w",
"test": "jest --passWithNoTests",
"test:watch": "jest --watch --coverage --passWithNoTests",
"test:coverage": "jest --coverage --passWithNoTests",
"test:ci": "npm run lint && npm run test",
"profile": "rimraf reports/ && mkdir reports && webpack --profile --json > reports/stats.json --config webpack.prod.js"
},
"engines": {
"node": ">= 8.9.1",
"npm": ">= 5.6.0"
},
"jest": {
"moduleNameMapper": {
"\\.(css|scss)$": "<rootDir>/config/tests/styleMock.js",
"^~/(.*)": "<rootDir>/src/$1"
},
"globals": {
"API_BASE_URL": "http://baseurl/"
},
"setupFilesAfterEnv": [
"<rootDir>/config/tests/setup.js"
]
},
"browserslist": [
"> 0.25%",
"not dead"
]
}
Is there a reason that css-loader alone is being bundled into the main bundle? And how do I either make it stop or resize it to a manageable level?
Be aware, running the production mode webpack build and having NODE_ENV set to production is two different thing! And without setting it, NODE_ENV ended up undefined, so style-loader was used for every build.
You have to do an export NODE_ENV=production; before running your build with your current code.
Alternatively you can create your webpack config like this:
module.exports = (env, argv) => {
if (argv.mode === 'development') {
}
if (argv.mode === 'production') {
}
return config;
};
This way you can manage it from cli simply by passing --mode=production or --mode=development and instead of env variables you can rely on webpack's configuration.

Returning 404 not found for the micro frontend dependencies from the host application

I am trying to implement a POC on micro frontends using module federation as an integration tool. I am able to run micro frontend separately(running on port 4001) but when I tried to run a host application(running on port 4000) that has microfrontend integrated into it, It was throwing errors saying that it couldn't able to found MF's dependencies i.e react-redux and redux toolkit as shown in the below image.
Host application doesn't have react-redux and redux toolkit as dependencies and I am not sure why it is trying to get dependencies from port 4000 instead of 4001. Can I know how to resolve this issue?
Host -webpack.config
const config: Configuration = {
mode: 'development',
output: {
publicPath: '/'
},
module: {
rules: [
{
test: /\.(ts|js)x?$/i,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
'#babel/preset-env',
'#babel/preset-react',
'#babel/preset-typescript'
]
}
}
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
plugins: [
new ModuleFederationPlugin({
name: 'container',
remotes: {
mf1: 'mf1#http://localhost:4001/remoteEntry.js'
},
shared: [deps]
}),
new HtmlWebpackPlugin({
template: 'public/index.html'
}),
new HotModuleReplacementPlugin(),
new ForkTsCheckerWebpackPlugin({
async: false
}),
new ESLintPlugin({
extensions: ['js', 'jsx', 'ts', 'tsx']
})
],
devtool: 'inline-source-map',
devServer: {
static: path.join(__dirname, 'build'),
historyApiFallback: true,
port: 4000,
open: true
}
};
export default config;
MF- webpack.config.js
const config: Configuration = {
mode: 'development',
output: {
publicPath: '/'
},
entry: './src/index',
module: {
rules: [
{
test: /\.(ts|js)x?$/i,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
'#babel/preset-env',
'#babel/preset-react',
'#babel/preset-typescript'
]
}
}
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
plugins: [
new ModuleFederationPlugin({
name: 'mf1',
filename: 'remoteEntry.js',
library: {type: 'var', name: 'mf1'},
exposes: {
'./Mf1App': './src/bootstrap'
},
shared: [deps]
}),
new HtmlWebpackPlugin({
template: 'public/index.html'
}),
new HotModuleReplacementPlugin(),
new ForkTsCheckerWebpackPlugin({
async: false
}),
new ESLintPlugin({
extensions: ['js', 'jsx', 'ts', 'tsx']
})
],
devtool: 'inline-source-map',
devServer: {
static: path.join(__dirname, 'build'),
historyApiFallback: true,
port: 4001,
open: true
}
};
export default config;
Host-package.json
{
"name": "container",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"start": "webpack serve --config config/webpack.dev.config.ts",
"build": "webpack --config config/webpack.prod.config.ts",
"lint": "eslint \"*/**/*.{js,ts,tsx}\"",
"lint:fix": "eslint \"*/**/*.{js,ts,tsx}\" --fix"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.15.8",
"#babel/plugin-transform-runtime": "^7.15.8",
"#babel/preset-env": "^7.15.8",
"#babel/preset-react": "^7.14.5",
"#babel/preset-typescript": "^7.15.0",
"#babel/runtime": "^7.15.4",
"#types/node": "^16.10.9",
"#types/react": "^17.0.29",
"#types/react-dom": "^17.0.9",
"#types/webpack": "^5.28.0",
"#types/webpack-dev-server": "^4.3.1",
"#typescript-eslint/eslint-plugin": "^5.0.0",
"#typescript-eslint/parser": "^5.0.0",
"babel-loader": "^8.2.2",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0",
"fork-ts-checker-webpack-plugin": "^6.3.4",
"html-webpack-plugin": "^5.3.2",
"prettier": "^2.4.1",
"ts-node": "^10.3.0",
"tsconfig-paths-webpack-plugin": "^3.5.1",
"webpack": "^5.58.2",
"webpack-cli": "^4.9.0",
"webpack-dev-server": "^4.3.1",
"#types/fork-ts-checker-webpack-plugin": "^0.4.5",
"clean-webpack-plugin": "^3.0.0",
"typescript": "^4.4.4",
"eslint-webpack-plugin": "^2.4.1"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
}
MF1-package.json
{
"name": "microfrontend1",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"start": "webpack serve --config config/webpack.dev.config.ts",
"build": "webpack --config config/webpack.prod.config.ts",
"lint": "eslint \"*/**/*.{js,ts,tsx}\"",
"lint:fix": "eslint \"*/**/*.{js,ts,tsx}\" --fix"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.15.8",
"#babel/plugin-transform-runtime": "^7.15.8",
"#babel/preset-env": "^7.15.8",
"#babel/preset-react": "^7.14.5",
"#babel/preset-typescript": "^7.15.0",
"#babel/runtime": "^7.15.4",
"#types/node": "^16.10.9",
"#types/react": "^17.0.29",
"#types/react-dom": "^17.0.9",
"#types/webpack": "^5.28.0",
"#types/react-redux": "^7.1.19",
"#types/webpack-dev-server": "^4.3.1",
"#typescript-eslint/eslint-plugin": "^5.0.0",
"#typescript-eslint/parser": "^5.0.0",
"babel-loader": "^8.2.2",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0",
"fork-ts-checker-webpack-plugin": "^6.3.4",
"html-webpack-plugin": "^5.3.2",
"prettier": "^2.4.1",
"ts-node": "^10.3.0",
"tsconfig-paths-webpack-plugin": "^3.5.1",
"webpack": "^5.58.2",
"webpack-cli": "^4.9.0",
"webpack-dev-server": "^4.3.1",
"#types/fork-ts-checker-webpack-plugin": "^0.4.5",
"clean-webpack-plugin": "^3.0.0",
"typescript": "^4.4.4",
"eslint-webpack-plugin": "^2.4.1"
},
"dependencies": {
"#reduxjs/toolkit": "^1.6.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.5"
}
}
In MF webpack.config.js update the output object as below:
`output: {
publicPath: 'http://localhost:4001/'
}`
Let me know if it helped.

Invalid hook call on npm link a library to application

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the render er (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app.
I am developing own library of components and using same library in my application through npm link but getting above error while testing.
here is my webpack.config
const path = require('path');
module.exports = {
entry: './src/index.ts',
devtool: 'eval-source-map',
module: {
rules: [
{
// "oneOf" will traverse all following loaders until one will
// match the requirements. When no loader matches it will fall
// back to the "file" loader at the end of the loader list.
oneOf: [
{
test: /\.(tsx|ts|mjs)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
plugins: [['#babel/plugin-proposal-class-properties'], ['#babel/plugin-transform-runtime', { "regenerator": true }]],
presets: ['#babel/preset-env', '#babel/preset-react', '#babel/preset-typescript'],
},
},
},
{
test: /\.mjs$/,
use: {
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env'],
},
},
},
{
test: /\.(css|scss)$/,
use: [{ loader: 'style-loader' }, { loader: 'css-loader' }, { loader: 'sass-loader' }],
},
// "url" loader works like "file" loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
// A missing `test` is equivalent to a match.
{
exclude: [/\.js$/, /\.ts$/, /\.html$/, /\.json$/],
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
},
},
],
},
],
},
resolve: {
extensions: ['.mjs', '.js', '.ts', '.tsx', '.json'],
},
output: {
path: path.resolve(__dirname, 'dist/'),
publicPath: '',
filename: 'bundle.js',
libraryTarget: 'commonjs'
}
};
here is my package.json
{
"name": "abc",
"version": "1.0.42",
"description": "A React component library",
"main": "dist/bundle.js",
"types": "dist/types/index.d.ts",
"scripts": {
"test": "jest",
"build:development": "npm-run-all --parallel webpack:watch generate-typings",
"build:production": "npm-run-all webpack:prod generate-typings",
"build:clean": "rmdir /Q /S dist",
"generate-typings": "tsc --declaration --emitDeclarationOnly",
"styleguide": "npx styleguidist server",
"styleguide:build": "npx styleguidist build",
"webpack:watch": "webpack --watch --mode=development",
"webpack:prod": "webpack --mode=production",
"localVersion": "node -p \"require('./package.json').version\"> localVersion.txt",
"serverVersion": "npm view #company/abc version > serverVersion.txt"
},
"repository": {
"type": "git",
"url": "url"
},
"keywords": [
"react",
"components",
"typescript"
],
"author": "Spencer Cousino",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.11.1",
"#babel/plugin-proposal-class-properties": "^7.10.4",
"#babel/preset-env": "^7.11.0",
"#babel/preset-react": "^7.10.4",
"#babel/preset-typescript": "^7.10.4",
"#fortawesome/fontawesome-svg-core": "^1.2.28",
"#fortawesome/free-solid-svg-icons": "^5.13.0",
"#types/enzyme": "^3.10.5",
"#types/fetch-mock": "^7.3.2",
"#types/jest": "^24.9.1",
"#types/nock": "^10.0.3",
"#types/node": "^11.15.9",
"#types/react": "^16.9.0",
"#types/react-dom": "^16.9.0",
"#types/react-table": "^6.8.7",
"async-wait-until": "^1.2.4",
"babel-loader": "^8.1.0",
"css-loader": "^1.0.1",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"fetch-mock": "^7.7.3",
"file-loader": "^3.0.1",
"identity-obj-proxy": "^3.0.0",
"jest": "^25.3.0",
"nock": "^10.0.6",
"node-fetch": "^2.6.1",
"node-sass": "^4.14.1",
"npm-run-all": "^4.1.5",
"oidc-client": "^1.10.1",
"powerbi-report-component": "^1.6.0",
"prettier": "1.17.0",
"react": "^16.14.0",
"react-docgen-typescript": "^1.20.1",
"react-dom": "^16.14.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-table": "6.10.3",
"react-slick": "^0.23.2",
"react-spinners": "^0.9.0",
"react-styleguidist": "^11.1.5",
"reactstrap": "^8.4.1",
"sass-loader": "^7.3.1",
"ts-jest": "^25.4.0",
"typescript": "^3.8.3",
"url-loader": "^1.1.2",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.12"
},
"dependencies": {
"#babel/plugin-transform-runtime": "^7.12.1",
"#babel/runtime": "^7.12.5",
"#company/abc": "0.0.6",
"#company/abc1": "^1.2.0",
"#company/abc2": "^1.0.23",
"#company/abc3": "^1.9.1",
"#company/abc4": "^1.0.1",
"core-js": "^3.8.0",
"date-fns": "^1.30.1",
"react-bingmaps": "^3.6.1",
"react-bootstrap": "^0.32.3",
"react-grid-layout": "^0.16.6",
"react-scripts": "^4.0.3",
"react-tabs": "^3.1.1",
"slick-carousel": "^1.8.1"
},
"peerDependencies": {
"react": "~16.8.4",
"react-dom": "~16.8.4",
"#types/node": "^11.11.6",
"node-fetch": "^2.3.0",
"oidc-client": "^1.7.0",
"powerbi-report-component": "^1.1.3",
"#fortawesome/fontawesome-svg-core": "^1.2.17",
"#fortawesome/free-solid-svg-icons": "^5.8.1",
"react-slick": "^0.23.2",
"react-spinners": "^0.5.3",
"reactstrap": "^8.0.0"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"setupFilesAfterEnv": [
"./testSetup.ts"
],
"moduleNameMapper": {
"^.+\\.(css|scss)$": "identity-obj-proxy",
"^.+\\.(svg)$": "<rootDir>/src/mocks/fileMock.js"
},
"moduleFileExtensions": [
"js",
"jsx",
"ts",
"tsx"
],
"collectCoverageFrom": [
"**/context-components/**/*.{js,jsx,ts,tsx}"
]
}
}
I have tried multiple ways but none of them worked for me.
Any help will be appreciated.

Uncaught TypeError: Object(...) is not a function with React, Formik and Webpack

I've been trying to use both Formik and React-Form-Hooks but both libraries throw the same error when compiled. I use Typescript as my language and Babel as transpiler.
Uncaught TypeError: Object(...) is not a function
which occurs at:
const FormGlobalContext = Object(react__WEBPACK_IMPORTED_MODULE_0__["createContext"])(null);
with Formik having FormikContext. I haven't done anything except import both of them (seperately), so I have no code to show.
package.json:
{
"name": "x",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"watch": "babel --watch src --out-dir dist --extensions .ts,.tsx",
"build": "webpack --mode development",
"tsc": "tsc"
},
"devDependencies": {
"#babel/cli": "^7.7.5",
"#babel/core": "^7.7.5",
"#babel/plugin-proposal-class-properties": "^7.7.4",
"#babel/plugin-proposal-object-rest-spread": "^7.7.4",
"#babel/plugin-syntax-dynamic-import": "^7.7.4",
"#babel/preset-env": "^7.7.6",
"#babel/preset-typescript": "^7.7.4",
"#babel/plugin-proposal-async-generator-functions": "^7.7.4",
"#babel/plugin-transform-async-to-generator": "^7.7.4",
"#babel/plugin-transform-regenerator": "^7.7.5",
"#babel/polyfill": "^7.7.0",
"#babel/preset-react": "^7.7.4",
"#types/jquery": "^3.3.31",
"#types/lodash": "^4.14.149",
"#types/react": "^16.9.17",
"#types/react-dom": "^16.9.4",
"#types/underscore": "^1.9.4",
"#types/yup": "^0.26.27",
"#types/delaunator": "^3.0.0",
"awesome-typescript-loader": "^5.2.1",
"babel-loader": "^8.0.6",
"babel-minify-webpack-plugin": "^0.3.1",
"react-hot-loader": "^4.12.18",
"typescript": "^3.7.3",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"babel-polyfill": "^6.26.0",
"uglifyjs-webpack-plugin": "^2.2.0",
"underscore": "^1.9.1"
},
"author": "",
"license": "ISC",
"dependencies": {
"jquery": "^3.4.1",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"react": "^15.1.1",
"react-dom": "^15.1.1",
"react-hook-form": "^3.29.4",
"yup": "^0.28.0"
}
}
.babelrc
{
"presets": [
["#babel/env", { "modules": false }],
"#babel/preset-react",
"#babel/typescript"
],
"plugins": [
"#babel/plugin-syntax-dynamic-import",
"#babel/proposal-class-properties",
"#babel/proposal-object-rest-spread",
"#babel/plugin-transform-regenerator",
"#babel/plugin-proposal-async-generator-functions",
"#babel/plugin-transform-async-to-generator"
]
}
webpack.config.js:
const path = require('path');
const webpack = require('webpack');
const MinifyPlugin = require("babel-minify-webpack-plugin");
module.exports = {
watch: true,
entry: './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'babel-loader',
exclude: /node_modules/,
sideEffects: true,
},
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
output: {
filename: 'tplanner.js',
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'umd',
umdNamedDefine: true
},
plugins: [
new MinifyPlugin({}, {}),
new webpack.HotModuleReplacementPlugin()
]
};
My React and React-Dom were outdated, I used npm upgrade react react-dom to fix it.

Webpack production build is returning development build everytime

On a ReactJS project, it is using webpack for building it. Everytime I run the script for production build, it is giving me a development build (getting This page is using the development build of React. from React Developer Tools). I need to get the production build.
The webpack has been configured by some other developer who's not around and I don't have any prior experience with webpack. So I am facing a really hard time to resolve the error.
package.json:
{
"name": "enquiry-form-rnd-2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "cross-env BUILD_ENV=dev webpack-dev-server --config ./webpack.config.js --mode development --progress --colors --port 3002",
"build:stage": "BUILD_ENV=STAGE webpack --mode production",
"build:prod": "BUILD_ENV=prod webpack --mode production"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.4.4",
"#babel/plugin-proposal-class-properties": "^7.4.4",
"#babel/preset-env": "^7.4.4",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.6",
"babel-plugin-transform-regenerator": "^6.26.0",
"concurrently": "^4.1.0",
"css-loader": "^2.1.1",
"gulp": "^4.0.2",
"gulp-autoprefixer": "^5.0.0",
"gulp-clean": "^0.4.0",
"gulp-install": "^1.1.0",
"gulp-open": "^3.0.1",
"gulp-sass": "^4.0.1",
"gulp-sourcemaps": "^2.6.4",
"node-sass": "^4.12.0",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"url-loader": "^1.1.2",
"webpack": "^4.31.0",
"webpack-cli": "^3.3.2",
"webpack-dev-server": "^3.3.1"
},
"dependencies": {
"babel-polyfill": "^6.26.0",
"cross-env": "^5.2.0",
"google-maps-react": "^2.0.2",
"immutable": "^4.0.0-rc.12",
"moment": "^2.24.0",
"numeral": "^2.0.6",
"react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.8",
"react-datetime": "^2.16.3",
"react-dom": "^16.8.6",
"react-intl": "^2.8.0",
"react-notifications-component": "^1.1.1",
"react-redux": "^7.0.3",
"react-responsive": "^6.1.2",
"redux": "^4.0.1",
"redux-devtools-extension": "^2.13.8",
"redux-logger": "^3.0.6",
"redux-saga": "^1.0.2",
"reselect": "^4.0.0",
"styled-components": "^4.2.0",
"styled-icons": "^7.14.0",
"validator": "^11.1.0",
"whatwg-fetch": "^3.0.0"
}
}
webpack.config.js:
const webpack = require("webpack");
module.exports = {
entry: ["babel-polyfill", "./src/index.js"],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader"]
},
{ test: /\.css$/, loader: "style-loader!css-loader" },
{
test: /\.scss$/,
use: [
"style-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS, using Node Sass by default
]
},
{
test: /\.(pdf|jpg|png|gif|svg|ico)$/,
use: [
{
loader: "url-loader"
}
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
loader: "file-loader"
}
]
},
resolve: {
extensions: ["*", ".js", ".jsx", "css"]
},
output: {
path: __dirname + "/build",
publicPath: "/",
filename: "bundle.js"
},
devServer: {
contentBase: "./dist",
host: "172.16.228.94",
// host: "192.168.2.108",
port: 3002
},
plugins: [
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
"process.env.DEBUG": JSON.stringify(process.env.BUILD_ENV),
"process.env.BUILD_ENV": JSON.stringify(process.env.BUILD_ENV)
})
]
};

Resources