React: Unable to set proxy server for react appllication - reactjs

I have my own react app and I tried adding proxy to the package.json, but it is not working.
On the other hand The simple create-react-app (as in the react basic example) works just fine but it does not have any webpack configured.
Here is my webpack:
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: 'body'
})
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve('./dist'),
filename: 'index_bundle.js'
},
module: {
loaders:[
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.jsx$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
loader: "style-loader!css-loader?modules"
},
{
test: /\.png$/,
loader: "url-loader?limit=100000"
},
{
test: /\.jpg$/,
loader: "file-loader"
},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/octet-stream'
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader'
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=image/svg+xml'
}
]
},
resolve: {
extensions: ['.js', '.jsx' ,'.json'],
modules: [path.join(__dirname, 'src'), 'node_modules']
},
plugins: [HtmlWebpackPluginConfig]
}
My package.json:
{
"name": "A2ZPressMaterial",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "webpack-dev-server --history-api-fallback",
"webpack-watch": "webpack -w",
"express-server": "node ./server",
"dev": "concurrently --kill-others \"npm run webpack-watch\" \"npm run express-server\"",
"test": "echo \"Error: no test specified\" && exit 1"
},
"proxy": "http://localhost:3001",
"dependencies": {
"classnames": "^2.2.5",
"css-loader": "^0.28.7",
"file-loader": "^0.11.2",
"html-webpack-plugin": "^2.30.1",
"http-proxy-middleware": "^0.17.4",
"material-ui": "^0.19.2",
"path": "^0.12.7",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-lazyload": "^2.2.7",
"react-redux": "^5.0.6",
"react-router-dom": "^4.2.2",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"style-loader": "^0.18.2",
"url-loader": "^0.5.9",
"webpack": "^3.5.6",
"webpack-dev-server": "^2.8.1"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"concurrently": "^3.5.0"
}
}
I get 404 on my ajax/fetch calls.
My express sever is running on 3001.
The simple create-react-app is able to hit this server.
I run the react app as yarn start and express server as npm start

Looks like the issue here is that you're trying to use the proxy setting using your own webpack setup.
The proxy setting is a feature of create-react-app so if you're making your own setup you'll need to implement that manually.
Note that the documentation for the proxy setting is found in the create-react-app documentation, not the React documentation:
https://create-react-app.dev/docs/proxying-api-requests-in-development/
Late answer in case anyone finds this question in the future.

Related

React localhost problem with http => https

I used the instructions from the official react documentation, but localhost still starts from http://.
My package.json file where I tried to add a solution.
I also tried 'set HTTPS = true && npm start'.
{
"name": "project",
"version": "1.0.0",
"description": "Project",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack -p",
"start": "webpack-dev-server --hot --inline -d && HTTPS=true react-scripts start",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#babel/polyfill": "^7.8.7",
"formik": "^2.1.4",
"ramda": "^0.27.0",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-https-redirect": "^1.1.0",
"react-iframe": "^1.8.0",
"react-router-dom": "^5.0.1",
"react-scripts": "^3.4.1",
"react-scroll-up-button": "^1.6.4",
"styled-components": "^5.0.1",
"whatwg-fetch": "^3.0.0"
},
"devDependencies": {
"#babel/core": "^7.7.5",
"#babel/plugin-proposal-async-generator-functions": "^7.8.3",
"#babel/plugin-proposal-class-properties": "^7.7.4",
"#babel/plugin-transform-async-to-generator": "^7.8.3",
"#babel/plugin-transform-regenerator": "^7.8.7",
"#babel/preset-env": "^7.7.6",
"#babel/preset-react": "^7.7.4",
"autoprefixer": "^9.6.1",
"babel-loader": "^8.0.6",
"css-loader": "^3.2.0",
"file-loader": "^4.2.0",
"gh-pages": "^2.2.0",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.12.0",
"postcss-loader": "^3.0.0",
"react-inlinesvg": "^1.2.0",
"sass-loader": "^7.2.0",
"style-loader": "^1.0.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0"
}
}
webpackconfig.js
const path = require("path");
const Html = require('html-webpack-plugin');
module.exports = {
entry: [
"whatwg-fetch",
"./js/index.js",
],
output: {
filename: "js/out.js",
path: path.resolve(__dirname, "build")
},
devServer: {
port: 3001,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
}
},
{
test: /\.scss$/,
use: [
'style-loader',
'css-loader',
{
loader: "postcss-loader",
options: {
plugins: () => [
require("autoprefixer")()
],
},
},
'sass-loader',
]
},
{
test: /\.(jpg|jpeg|gif|png)$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
publicPath: 'images',
outputPath: 'images',
}
}
},
{
test: /\.(eot|ttf|woff|woff2)$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
publicPath: 'fonts',
outputPath: 'fonts',
}
}
},
]
},
plugins: [
new Html({
filename: 'index.html',
template: './index.html',
})
]
};
I'm a beginner and I can't deal with it, if someone can help me, I will be very grateful.
The options for SET HTTPS=true are for projects that are made with create-react-app. Since you have a webpack configuration file of your own, you have to change the configuration for enabling HTTPS.
You can do this with:
devServer: {
https: true
}
This enables a self signed certificate. You can provide a certifiate with:
devServer: {
https: true,
key: fs.readFileSync('/path/to/server.key'),
cert: fs.readFileSync('/path/to/server.crt'),
ca: fs.readFileSync('/path/to/ca.pem'),
}
Docs
In package.json try add set HTTPS=true instead of HTTPS=true in start script.

Module not found: Error: Cannot resolve module 'babel-loader' in 'C:\Users\durgat\reactApp'

{
"name": "reactApp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack --mode development",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"react": "^16.7.0",
"react-dom": "^16.7.0",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-dev-server": "^3.1.14"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^8.0.5",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"html-webpack-plugin": "^3.2.0"
}
}
Please help me to solve this issue , i am still getting this issue after doing many changes like , i degraded the babel to lowest version & i installed npm using this command and also i did many changes but there is no result.
And webpack.config.js file
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './main.js',
output: {
path: path.join(__dirname, '/bundle'),
filename: 'index_bundle.js'
},
devServer: {
inline: true,
port: 8080
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
},
plugins:[
new HtmlWebpackPlugin({
template: './index.html'
})
]
}
i think this could help as i dont know what is webpack file configration
but you can give it try in your webpack file
resolveLoader: {
root: path.join(__dirname, 'node_modules')
}
please share your webpack.config file

Module not found: Can't resolve custom package [React Import]

I am trying to create my very first node package, it was successfully publish and live on npmjs.com website but whenever i include it on a sample react project using create-react-app i get this following error
Here's how i import it
import IconTooltip from 'react-icons-tooltip';
Here's the error
Module not found: Can't resolve './react-icons-tooltip' in '/home/ubuntu/workspace/lasting/src/Components'
This is the Package package.json file
{
"name": "react-icons-tooltip",
"version": "1.0.4",
"description": "",
"main": "./lib/IconTooltip.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --progress --colors --p",
"transpile": "node_modules/.bin/babel ./src/index.js --presets babel-preset-es2015 --out-file lib/IconTooltip.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.3",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"webpack": "^4.0.1",
"webpack-cli": "^2.0.10"
},
"dependencies": {
"prop-types": "^15.6.1",
"react": "^16.2.0",
"react-dom": "^16.2.0"
}
}
webpack.config.js
const webpack = require("webpack");
module.exports = {
entry: './src/index.js',
output: {
path: __dirname + '/build',
filename: 'IconToolTip.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.css$/,
loaders: ['style-loader', 'css-loader']
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
})
]
};
If i import the local the package seems to work fine
import IconTooltip from './lib/IconTooltip';
TIA!
I had that same problem before and that fixed it. I checked other modules and they all enter in index.js, is all I can say :)

Webpack - Build css not applied

I am new to ReactJS. I am trying to use scss in react using sass-loaders
My webpack config looks like this-
var webpack = require('webpack');
var path = require('path');
var APP_DIR = path.resolve(__dirname + 'react-js/src/');
var BUILD_DIR = path.resolve(__dirname, 'react-js/dist/');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry: './react-js/src/index.jsx',
output: {
path: BUILD_DIR,
publicPath: '/dist',
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx']
},
module: {
loaders: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader',
publicPath: '/dist'
})
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader!sass-loader',
publicPath: '/dist'
})
},
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['react', 'es2015']
}
}
]
},
plugins: [
new ExtractTextPlugin('styles.css'),
]
}
All these configurations were from there respective documentations.
.
When I run webpack-dev-server it shows following output-
This is the screen when I save a scss file -
Note - I have removed the ExtractTextPlugin from my webpack config file.
Directory structure -
I just don't realise how it's not imported to the rendered react app. I thought importing the scss file in index.jsx like shown below will make the css file it's dependency, but it doesn't work.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './app';
import style from '../../scss/main.scss';
ReactDOM.render(
<App />,
document.getElementById('root')
);
Why?
Edited
Package.json
{
"name": "skippo-vendor-admin-webui",
"version": "1.0.0",
"description": "",
"main": "./react-js/src/index.jsx",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "./node_modules/.bin/webpack -d --progress --colors",
"prod": "./node_modules/.bin/webpack -p --progress --colors",
"watch": "./node_modules/.bin/webpack -d --progress --colors --watch",
"start": "./node_modules/.bin/webpack-dev-server --progress --colors --hot --inline --contentBase './react-js/'"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"chai": "^3.5.0",
"css-loader": "^0.28.1",
"extract-text-webpack-plugin": "^2.1.0",
"karma": "^1.6.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.1.1",
"karma-mocha": "^1.3.0",
"karma-sinon": "^1.0.5",
"karma-webpack": "^2.0.3",
"mocha": "^3.3.0",
"node-sass": "^4.5.2",
"sass-loader": "^6.0.3",
"sinon": "^2.2.0",
"style-loader": "^0.17.0"
},
"dependencies": {
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-redux": "^5.0.4",
"react-router": "^4.1.1"
}
}
The ExtractTextPlugin should only be used for your production build, since it will create a separate compiled css file that can be served without any special treatment.
For development, you can use this rule instead:
{
test: /\.s?css$/,
use: ['style-loader', 'css-loader', 'sass-loader'],
}
Install the 3 loaders as a dev dependency if you don't have them.
Also you don't need to specify a name when you import a scss file, just import it.
import '../../scss/main.scss';
Change this line
import style from '../../scss/main.scss';
With
import '../../scss/main.scss';

ReactDOM issue: Module not found: Error: Cannot resolve module 'react/lib/ReactDOM'

I just restarted my computer and now when I try to run my React project locally I get this error. Before restarting I was able to work on my project just fine. I tried removing my node_modules folder and npm installing again, still not working. Anybody have any ideas what my problem could be? Here is my package.json and webpack.config:
package.json:
{
"version": "1.0.0",
"description": "== README",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Stefan",
"license": "ISC",
"dependencies": {
"babel-core": "6.18.2",
"babel-loader": "6.2.7",
"babel-preset-es2015": "6.18.0",
"babel-preset-react": "6.16.0",
"css-loader": "0.26.1",
"file-loader": "0.9.0",
"less": "2.3.1",
"less-loader": "2.2.3",
"react": "15.4.1",
"react-addons-test-utils": "15.4.1",
"react-dom": "15.3.2",
"react-fontawesome": "1.5.0",
"react-redux": "4.4.5",
"react-router": "3.0.0",
"redux": "3.6.0",
"style-loader": "0.13.1",
"url-loader": "0.5.7",
"webpack": "1.13.3"
}
}
webpack.config:
module.exports = {
context: __dirname,
entry: "./frontend/app.jsx",
output: {
path: "app/assets/javascripts",
filename: "bundle.js",
publicPath: "/assets/"
},
resolve: {
extensions: ["", ".js", ".jsx"]
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
},{
test: /\.node$/,
loader: "node-loader"
},{
test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/font-woff"
}, {
test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/font-woff"
}, {
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/octet-stream"
}, {
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: "file"
}, {
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=image/svg+xml"
}
]
},
devtool: 'source-map',
};

Resources