Tag Archives: react

How to Fix webpack Module build failed Error: The node API for ‘babel’ has been moved to babel-core

Red when webpack is packaged:

Module build failed: Error: The node API for 'babel' has been moved to babel-core

There is nothing available on the web,
so delete the entire node_modules folder,
download the dependency package again:

npm init;
cnpm install --save babel-loader babel-preset-es2015 babel-preset-react babel-core

And to pack:

webpak

Ok, perfect!

React runs the browser and reports an error [A may have only one child element]

The error message

Uncaught Invariant Violation: A <Router> may have only one child element
    at invariant (http://localhost:8111/bundle.js:1071:15)
    at Router.componentWillMount (http://localhost:8111/bundle.js:2413:54)
    at callComponentWillMount (http://localhost:8111/bundle.js:27042:14)
    at mountClassInstance (http://localhost:8111/bundle.js:27135:5)
    at updateClassComponent (http://localhost:8111/bundle.js:30309:5)
    at beginWork (http://localhost:8111/bundle.js:31265:16)
    at performUnitOfWork (http://localhost:8111/bundle.js:34933:12)
    at workLoop (http://localhost:8111/bundle.js:34973:24)
    at renderRoot (http://localhost:8111/bundle.js:35056:7)
    at performWorkOnRoot (http://localhost:8111/bundle.js:35963:7)

The solution

The original demo

ReactDom.render(
    <BrowserRouter>
	    <Nav />
	    {getRouter()}
    </BrowserRouter>,
    document.getElementById('app')
)

Resolved Demo (if there are multiple components in The BrowserRouter, add a layer of div to the BrowserRouter)

ReactDom.render(
    <BrowserRouter>
        <div>
            <Nav />
            {getRouter()}
        </div>
    </BrowserRouter>,
    document.getElementById('app')
)

As for the reasons, I haven’t studied them in detail yet. If you know some friends, you can communicate with them, or explain them in the comments below. Thank you.

React Native Network Request Failed solution

Today, when I was writing a demo of Network Request with React Native, the emulators kept telling me Network Request Failed, which was very annoying, baidu search for a long time did not find the answer I wanted, someone said on stackoverflow that this error only appeared in the development mode, but the production version would not appear, but it didn’t solve the problem at all. Another says: If you’re using FETCH to get data, and you’re using POST, notice that Headers has to add a request header. You can’t use the body when the request is for GET, you must include the body when it is for POST, and once you set the header, everything works fine. However, the fetch method used by me does not need to add any headers, so the problem of Network Request Failed has not been solved.
To prevent code errors, I directly copied the official sample code, as follows

fetch('http://facebook.github.io/react-native/movies.json')
      .then((response) => response.json())
      .then((responseJson) => {
        console.log(responseJson.movies);
        return responseJson.movies;
      })
      .catch((error) => {
        console.error(error);
      });

I put this code directly into the browser console console to run, and found that everything was ok, the console successfully output content. Then it could be the ios emulator.
Now that fecth API is problematic try using XMLHttpRequest instead, and the console output the Resource could not be loaded because the app Transport Security policy requires the use of a secure Connection, Baidu search only found that iOS9 introduced the new feature App Transport Security (ATS). The new feature requires that networks accessed within the App must use the HTTPS protocol, meaning that the Api interface must be HTTPS in the future. But my project USES HTTP, and I can’t immediately switch to HTTPS for transport.
Fortunately there is an alternative solution
1. Add NSAppTransportSecurity type Dictionary to info.plist.
2. Add nsallowsarbitraryoccurrence type Boolean under NSAppTransportSecurity, value set to YES
For details, please refer to this document for the resolution that iOS9 HTTP does not work properly

webstorm npm install –save –save-exact –loglevel error react react-dom react-scripts has failed.

webstorm create react project pull data

project right click –> Open in terminal and then type the following command in the terminal

set up taobao image of acceleration: NPM config set registry https://registry.npm.taobao.org

check whether the setting is successful: NPM config get registry
details are as follows:

Microsoft Windows [版本 10.0.17763.615]
(c) 2018 Microsoft Corporation。保留所有权利。

H:\Project\React app\untitled3>npm config set registry https://registry.npm.taobao.org

H:\Project\React app\untitled3>npm config get registry
https://registry.npm.taobao.org/

H:\Project\React app\untitled3>


React Missing class properties transform.

缺少类属性变换。

描述:

ERROR in ./app/components/MsgComponent/msgAdd.js
Module build failed: SyntaxError: Missing class properties transform.

  73 |  }
  74 |
> 75 |     handleClearContent = () => {
     |     ^
  76 |         this.setState({
  77 |             editorState:''
  78 |         })

address:

Webpack — module build failed: error: the node API for ‘Babel’ has been moved to Babel core

webpack is red when packaged:

Module build failed: Error: The node API for 'babel' has been moved to babel-core

there is nothing available on the web,
so delete the entire node_modules folder,
to download the dependency package again:

npm init;
cnpm install --save babel-loader babel-preset-es2015 babel-preset-react babel-core

and then pack:

webpak

ok, perfect!

Webpack error module build failed: typeerror: fileSystem.statSync is not a function

follow the video operation, delete node_modules directory, webpack

ERROR in ./src/js/index.js
Module build failed: TypeError: fileSystem.statSync is not a function
    at module.exports (/Users/mac/source/9-01/node_modules/babel-loader/lib/utils/exists.js:7:25)
    at find (/Users/mac/source/9-01/node_modules/babel-loader/lib/resolve-rc.js:13:9)
    at Object.module.exports (/Users/mac/source/9-01/node_modules/babel-loader/lib/index.js:113:132)

Baidu once, someone said because
webpack version inconsistency results. So reinstall

npm install webpack --save-dev

There are many warnings when performing this step

npm notice save webpack is being moved from dependencies to devDependencies
npm WARN [email protected] requires a peer of webpack@2 || 3 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of jquery@>=1.8.0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

The babel-Loader version requires a more advanced WebPack, so execute the following statement to install version 3.0

npm install [email protected] --save-dev

Execute the WebPack and it will run successfully.