Invalid hook call on npm link a library to application - reactjs

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.

Related

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

**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.

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.

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)
})
]
};

React Unexpected token < - babel loader

Hi i have problem with babel-loader it write me
ERROR in ./src/js/pages/Admin/views/Pages/Register/Register.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: C:\Projekty\eshop_frontend_template\src\js\pages\Admin\views\Pages\Register\Register.js: Unexpected token (7:6)
> 5 | render() {
6 | return (
> 7 | <div className="app flex-row align-items-center">
| ^
8 | <Container>
9 | <Row className="justify-content-center">
10 | <Col md="6">
at Parser.raise (C:\Projekty\eshop_frontend_template\node_modules\#babel\parser\lib\index.js:3939:15)
at Parser.unexpected (C:\Projekty\eshop_frontend_template\node_modules\#babel\parser\lib\index.js:5248:16)
at Parser.parseExprAtom (C:\Projekty\eshop_frontend_template\node_modules\#babel\parser\lib\index.js:6328:20)
at Parser.parseExprSubscripts (C:\Projekty\eshop_frontend_template\node_modules\#babel\parser\lib\index.js:5924:21)
at Parser.parseMaybeUnary (C:\Projekty\eshop_frontend_template\node_modules\#babel\parser\lib\index.js:5903:21)
at Parser.parseExprOps (C:\Projekty\eshop_frontend_template\node_modules\#babel\parser\lib\index.js:5812:21)
at Parser.parseMaybeConditional (C:\Projekty\eshop_frontend_template\node_modules\#babel\parser\lib\index.js:5784:21)
at Parser.parseMaybeAssign (C:\Projekty\eshop_frontend_template\node_modules\#babel\parser\lib\index.js:5731:21)
at Parser.parseParenAndDistinguishExpression (C:\Projekty\eshop_frontend_template\node_modules\#babel\parser\lib\index.js:6474:28)
at Parser.parseExprAtom (C:\Projekty\eshop_frontend_template\node_modules\#babel\parser\lib\index.js:6284:21)
# ./src/js/pages/Admin/views/Pages/index.js 37:39-60
# ./src/js/pages/Admin/Admin.js
# ./src/index.js
# multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./src/index.js
5 | render() {
6 | return (
> 7 | <div className="app flex-row align-items-center">
| ^
8 | <Container>
10 | <Col md="6">
my webpack.config.js
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
plugins: ['#babel/proposal-class-properties']
}
}
},
{ test: /\.css$/, loader: "style-loader!css-loader" },
{ test: /\.json$/, loader: "json-loader" },
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader? limit=100000' },
{
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
]
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
template: './dist/index.html'
})
],
my package.json#babel
"babel": {
"presets": [
[
"#babel/preset-env",
{
"modules": "commonjs",
"targets": {
"node": "current"
}
}
],
"#babel/preset-react"
],
"plugins": [
[
"#babel/plugin-proposal-decorators",
{
"legacy": true
}
]
]
},
my dependencies
"devDependencies": {
"#babel/core": "^7.1.0",
"#babel/preset-env": "^7.1.0",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.2",
"css-loader": "^1.0.0",
"react-hot-loader": "^4.3.11",
"style-loader": "^0.23.0",
"webpack": "^4.20.1",
"webpack-cli": "^3.1.1",
"webpack-dev-server": "^3.1.9"
},
"dependencies": {
"#babel/plugin-proposal-class-properties": "^7.1.0",
"#babel/plugin-proposal-decorators": "^7.1.0",
"#babel/polyfill": "^7.0.0",
"#coreui/coreui": "^2.0.9",
"#coreui/react": "^2.0.5",
"axios": "^0.18.0",
"css-loader": "^1.0.0",
"file-loader": "^2.0.0",
"flag-icon-css": "^3.2.0",
"font-awesome": "^4.7.0",
"html-webpack-plugin": "^3.2.0",
"json-loader": "^0.5.7",
"node-sass": "^4.9.3",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-localize-redux": "^3.4.0",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"redux": "^4.0.0",
"redux-localstorage-simple": "^2.1.4",
"redux-logger": "^3.0.6",
"redux-promise-middleware": "^5.1.1",
"redux-thunk": "^2.3.0",
"sass-loader": "^7.1.0",
"simple-line-icons": "^2.4.1",
"style-loader": "^0.23.0",
"universal-cookie": "^3.0.4"
}
I tried some helps on forums but nothing helped. Can you say me please what i have wrong?
I am using Babel 7
Thank you :)
I checked on my notebook, You have just lot of entries missing in package.json and You are using not supported features. After changing these files run 'npm install' and npm start should wotk :)
package.json
{
"name": "eshop_frontend_template",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --config ./webpack.config.js --mode development",
"build": "webpack -p --progres --config ./webpack.config.js --mode production",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://d3tr1tus#bitbucket.org/d3tr1tus/eshop_frontend_template.git"
},
"keywords": [],
"author": "Filip Březina <filip.brezina11#gmail.com> (localhost:8080)",
"license": "MIT",
"babel": {
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
[
"#babel/plugin-proposal-decorators",
{
"legacy": true
}
]
]
},
"devDependencies": {
"#babel/core": "^7.1.0",
"#babel/plugin-syntax-dynamic-import": "^7.0.0",
"#babel/preset-env": "^7.1.0",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.2",
"css-loader": "^1.0.0",
"html-webpack-plugin": "^3.2.0",
"react-hot-loader": "^4.3.11",
"style-loader": "^0.23.0",
"url-loader": "^1.1.1",
"webpack": "^4.20.1",
"webpack-cli": "^3.1.1",
"webpack-dev-server": "^3.1.9"
},
"dependencies": {
"#babel/plugin-proposal-class-properties": "^7.1.0",
"#babel/plugin-proposal-decorators": "^7.1.0",
"#babel/polyfill": "^7.0.0",
"#coreui/coreui": "^2.0.9",
"#coreui/coreui-plugin-chartjs-custom-tooltips": "^1.2.0",
"#coreui/react": "^2.0.5",
"axios": "^0.18.0",
"chart.js": "^2.7.2",
"css-loader": "^1.0.0",
"file-loader": "^2.0.0",
"flag-icon-css": "^3.2.0",
"font-awesome": "^4.7.0",
"json-loader": "^0.5.7",
"node-sass": "^4.9.3",
"react": "^16.5.2",
"react-chartjs-2": "^2.7.4",
"react-dom": "^16.5.2",
"react-loadable": "^5.5.0",
"react-localize-redux": "^3.4.0",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"reactstrap": "^6.4.0",
"redux": "^4.0.0",
"redux-localstorage-simple": "^2.1.4",
"redux-logger": "^3.0.6",
"redux-promise-middleware": "^5.1.1",
"redux-thunk": "^2.3.0",
"sass-loader": "^7.1.0",
"simple-line-icons": "^2.4.1",
"style-loader": "^0.23.0",
"universal-cookie": "^3.0.4"
}
}
webpack.config.js
const webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: "./src/index.js",
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env','#babel/react'],
plugins: ['#babel/proposal-class-properties', '#babel/plugin-proposal-object-rest-spread', '#babel/plugin-syntax-dynamic-import']
}
}
},
{ test: /\.css$/, use: [ 'style-loader', 'css-loader' ]},
{ test: /\.json$/, loader: "json-loader" },
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' },
{
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
]
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
output: {
path: __dirname + '/dist',
publicPath: '/',
filename: 'bundle.js'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
template: './dist/index.html'
})
],
devServer: {
contentBase: './dist',
hot: true
}
};
I found that the error is because of older version of babel which doesn't handle newer versions of react code.
Here's an easy fix :
My problem was of older babel version fixed easily by installing :
npm i #babel/plugin-proposal-class-properties #babel/preset-react #babel/preset-env #babel/core #babel/plugin-transform-runtime --save-dev
In .babelrc file (or in package.json inside "babel" : ) :
{
"presets": [
"#babel/react" ,
"#babel/env" ,
],
"plugins": [
"#babel/plugin-proposal-class-properties"
]
}
Now babel built it successfully after this.
In my case, I only needed the file .babelrc :
{
"presets": ["react"]
}

Babel polyfill doesn't work on Internet Explorer 11

I use in project react library with typescript and ES6 version of javascript. Some ES6 features are not supported by IE11 so I have to use Babel, however my application isn't correctly loaded in IE11
It throws a syntax error here
What cause this problem? This should be solved using this package transform-es2015-arrow-functions or is there some different problem which I overlooked?
package.json
{
"name": "SkodaAuto.C3PO.AdminConsole.Web",
"private": true,
"version": "0.0.0",
"devDependencies": {
"#babel/core": "^7.1.0",
"#babel/preset-env": "^7.1.0",
"#types/webpack": "2.2.15",
"#types/webpack-env": "1.13.0",
"aspnet-webpack": "2.0.1",
"aspnet-webpack-react": "3.0.0",
"awesome-typescript-loader": "3.2.1",
"babel-core": "^6.17.0",
"babel-loader": "^8.0.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-eval": "^6.22.0",
"babel-preset-es2015": "^6.16.0",
"babel-preset-react": "^6.16.0",
"css-loader": "0.28.4",
"eventsource-polyfill": "^0.9.6",
"extract-text-webpack-plugin": "2.1.2",
"file-loader": "^2.0.0",
"less": "3.7.1",
"less-loader": "4.1.0",
"node-noop": "1.0.0",
"style-loader": "0.18.2",
"typescript": "3.0.1",
"url-loader": "^1.1.1",
"webpack": "^4.19.1",
"webpack-cli": "^3.1.1",
"webpack-hot-middleware": "2.18.2",
"webpack-merge": "4.1.0"
},
"dependencies": {
"#babel/polyfill": "^7.0.0",
"#progress/kendo-data-query": "1.4.1",
"#progress/kendo-drawing": "1.5.7",
"#progress/kendo-react-buttons": "1.2.0",
"#progress/kendo-react-dateinputs": "1.2.0",
"#progress/kendo-react-dialogs": "1.2.0",
"#progress/kendo-react-dropdowns": "1.2.0",
"#progress/kendo-react-grid": "1.2.0",
"#progress/kendo-react-inputs": "1.2.0",
"#progress/kendo-react-intl": "1.2.0",
"#progress/kendo-react-layout": "1.2.0",
"#progress/kendo-react-pdf": "1.2.0",
"#progress/kendo-theme-default": "2.54.1",
"#telerik/kendo-intl": "^1.4.2",
"#types/deep-equal": "1.0.1",
"#types/history": "4.6.0",
"#types/prop-types": "15.5.3",
"#types/react": "16.4.2",
"#types/react-dom": "16.0.7",
"#types/react-hot-loader": "3.0.3",
"#types/react-redux": "4.4.45",
"#types/react-router": "4.0.12",
"#types/react-router-dom": "4.0.5",
"#types/react-router-redux": "5.0.3",
"babel-polyfill": "^6.26.0",
"bootstrap-less-port": "0.3.0",
"deep-equal": "1.0.1",
"history": "4.6.3",
"jquery": "3.0.0",
"query-string": "6.1.0",
"react": "16.4.2",
"react-appinsights": "1.0.4",
"react-dom": "16.4.2",
"react-hot-loader": "3.0.0-beta.7",
"react-redux": "5.0.5",
"react-resize-detector": "3.1.1",
"react-router-dom": "4.1.1",
"react-router-redux": "5.0.0-alpha.6",
"redux": "3.7.1",
"redux-form": "7.4.2",
"redux-logger": "3.0.6",
"redux-thunk": "2.2.0",
"serialize-error": "2.1.0"
},
"scripts": {
"build": "webpack --config webpack.config.js",
"build.vendor": "webpack --config webpack.config.vendor.js"
}}
webpack.config.js
const path = require('path');
const webpack = require('webpack');
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
const clientBundleOutputDir = './wwwroot/dist';
module.exports = {
devtool: 'inline-source-map',
entry: { 'main-client': './ClientApp/boot-client.tsx' },
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
modules: [
'./node_modules',
'./ClientApp'
]
},
mode: `development`,
output: {
filename: '[name].js',
publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{
test: /\.tsx?$/, include: [path.resolve(__dirname, clientBundleOutputDir)],
use: [{
loader: 'babel-loader',
options: {
presets: ['#babel-preset-env', '#babel/preset-react', 'es2015', "#babel/typescript"],
plugins: ["#babel/proposal-class-properties",
"#babel/proposal-object-rest-spread",
"transform-eval",
'transform-es2015-arrow-functions']
}
}]
},
{ test: /\.json$/, loader: "json-loader" },
{ test: /\.tsx?$/, exclude: /(node_modules|bower_components)/, include: /ClientApp/, use: 'awesome-typescript-loader?silent=true' },
{ test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' },
{
test: /\.less$/, use: [{
loader: 'style-loader' // creates style nodes from JS strings
}, {
loader: 'css-loader' // translates CSS into CommonJS
}, {
loader: 'less-loader' // compiles Less to CSS
}]
},
{
test: /\.(woff|woff2|ttf|eot)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
}
}
]
}
]
}};

Resources