Webpack build fails with Gitlab-CI

2019-08-20 06:49发布

问题:

When Gitlab-CI is running my pipeline I get an error in the console:

$ npm run build

> project-name@1.0.0 build /home/gitlab-runner/builds/1e932413/0/namespace/project-name
> webpack

using environment 'development'
Hash: 4eb3a28e1a75bb9f8fb5
Version: webpack 3.11.0
Time: 70ms
   Asset     Size  Chunks             Chunk Names
index.js  2.75 kB       0  [emitted]  main
   [0] ./index.js 261 bytes {0} [built] [failed] [1 error]

ERROR in ./index.js
Module parse failed: Unexpected token (10:2)
You may need an appropriate loader to handle this file type.
| const root = props => {
|   return(
|       <Provider store={store}>
|           <MyComponent {...props}/>
|       </Provider>
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! project-name@1.0.0 build: `webpack`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the project-name@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/gitlab-runner/.npm/_logs/2018-02-14T11_07_48_546Z-debug.log
ERROR: Job failed: exit status 1

I really don't know what causes this error. This is what I have tried so far:

  • SSH into the server and run webpack gives the same error
  • git status in the workspace reports no changes
  • copy the workspace somewhere else and running webpack is working
  • cloning a fresh copy somewhere else on the server and running webpack works too

回答1:

I solved this issue by changing the babel-loader configuration from

rules: [
    {
        test: /\.js$/,
        include: path.resolve(__dirname, 'src'),
        exclude: /(node_modules|bower_components|build)/,
        use: {
            loader: 'babel-loader',
            options: {
                presets: ['env']
            }
        }
    },
    ...
]

to

rules: [
    {
        test: /\.js$/,
        include: path.resolve(__dirname, 'src'),
        exclude: '/node_modules/',
        use: {
            loader: 'babel-loader',
            options: {
                presets: ['env']
            }
        }
    },
    ...
]

https://github.com/babel/babel-loader/issues/370#issuecomment-310378407