CI/CD on gitlab fails to compile with babel-loader

Viewed 1445

I have a react app with webpack. Dev server works as expected. When I run locally webpack --mode production it builds and creates a js bundle as expected. When I push it to gitlab it fails to build it with error:

ERROR in ./src/index.js 20:16
Module parse failed: Unexpected token (20:16)
You may need an appropriate loader to handle this file type.
| firebase.initializeApp(config);
| 
> ReactDOM.render(<App />, document.getElementById('app'));
| 

These are my devDependencies:

"devDependencies": {
  "@babel/core": "^7.2.2",
  "@babel/plugin-transform-flow-strip-types": "^7.2.3",
  "@babel/plugin-proposal-class-properties": "^7.2.3",
  "@babel/plugin-proposal-decorators": "^7.2.3",
  "@babel/plugin-proposal-object-rest-spread": "^7.2.0",
  "@babel/preset-env": "^7.2.3",
  "@babel/preset-react": "^7.0.0",
  "babel-eslint": "8.2.6",
  "babel-loader": "^8.0.4",
  "copy-webpack-plugin": "4.6.0",
  "css-loader": "0.28.11",
  "eslint": "4.19.1",
  "eslint-loader": "2.1.1",
  "eslint-plugin-react": "7.11.1",
  "html-webpack-plugin": "3.2.0",
  "style-loader": "0.21.0",
  "webpack": "^4.28.2",
  "webpack-cli": "^3.1.2",
  "webpack-dev-server": "^3.1.14"
}

And this is my webpack config:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.join(__dirname, 'build'),
    filename: 'index.bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: [/node_modules/, /build/],
        use: ['babel-loader', 'eslint-loader']
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      }
    ]
  },
  devServer: {
    historyApiFallback: true
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
      filename: 'index.html'
    }),
    new CopyWebpackPlugin([
      { from: path.join(__dirname, 'public/'), to: path.join(__dirname, 'build') }
    ])
  ]
};

This was working previously but I updated the libraries was out of date quite much. After update the gitlab ci fails.

This is the build config:

image: node:latest

cache:
    paths:
        - node_modules/

stages:
    - setup
    - build

setup:
    stage: setup
    script:
        - echo "Setup started"
        - yarn install
    artifacts:
        paths:
            - node_modules/

build:
    stage: build
    script: 
        - yarn build
    artifacts:
        paths:
            - build/

EDIT I have transpired index.js so maybe the entry is not good or something, but then I got a bunch of errors inside components that it cannot resolve jsx tags. I suspect babel-loader is not working on gitlab-ci.

EDIT 29.3.2019 I haven't find a solution for this. As of you can have free private repos on github, migrated the project to github and setup circleci. Circleci work for me flawlessly.

2 Answers

Add

"react": "^16.4.1",
"react-dom": "^16.4.1"

To your devDependencies and it should work.

The error is coming since you have not provided the react and react-dom module in the package.json.

Let me know if it works for you if not we will look forward for the issue

UPDATE:

image: node:latest

cache:
    paths:
        - node_modules/

stages:
    - setup
    - build

setup:
    stage: setup
    script:
        - echo "Setup started"
        - yarn install
    artifacts:
        paths:
            - node_modules/

build:
    stage: build
    script: 
        - yarn install react
        - yarn install react-dom
        - yarn build
    artifacts:
        paths:
            - build/

Adding the installation command directly to the script should solve this problem. Somehow your webpack is not finding the installation directory of the react and react-dom so passing the installation command will explicitly provide the module while building.

I just had the same issue with Gitlab CI and found out that the exclude was the problem:

exclude: [/node_modules/, /build/]

Try replacing it with the following:

exclude: '/node_modules/',
include: [
    path.join(__dirname, 'src')
]

It now works locally AND on Gitlab CI for me. Before, it only worked locally.

There is another issue that pointed me to the right direction: Webpack build fails with Gitlab-CI

Related