Jenkins自动化部署失败:Syntax Error: TypeError: Cannot read property 'range' of null

1,008 阅读1分钟

背景

昨晚照常提交测试环境代码,发现jenkins自动化部署报错Syntax Error: TypeError: Cannot read property 'range' of null,但是在本地npm run build都能正常打包没有错误,网上查找也是各种不一样的回答,适用不了我这边代码的情况

定位问题

报错信息如下:

`[4/4] Building fresh packages... success Saved lockfile. Done in 56.77s.

  • /usr/local/bin/yarn alpha yarn run v1.22.0 $ vue-cli-service build --mode alpha
  • Building for alpha... ERROR Failed to compile with 1 error10:17:28 AM

error in ./src/mixins/chartSettings.js

Syntax Error: TypeError: Cannot read property 'range' of null Occurred while linting /data/tool/jenkins/workspace/dms-oacbi-vue-dev/src/mixins/chartSettings.js:175 at Array.forEach () at Array.forEach ()

@ ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/data-magic/magic/index.vue?vue&type=script&lang=js& 567:0-51 591:24-37 @ ./src/views/data-magic/magic/index.vue?vue&type=script&lang=js& @ ./src/views/data-magic/magic/index.vue @ ./src/config/router.config.js @ ./src/router/index.js @ ./src/main.js @ multi ./src/main.js

ERROR Build failed with errors. error Command failed with exit code 1. info Visit yarnpkg.com/en/docs/cli… for documentation about this command. Build step 'Execute shell' marked build as failure Finished: FAILURE`

首先能出错误日志中定位到至少是chartSettings.js出了错误,但是175行并没有forEach之类的代码,而且是一些常规代码,所以175行这个信息是不准确的,我估计是打包之后的行数 所以我考虑是不是我这边用了一些新语法糖造成的版本问题,因为本地打包是没有报错的,我猜想可能是babel的版本出了纰漏,我代码中确实使用了ES2020的可选链语法,所以第一考虑是将chartSettings.js中的可选链改回ES5的代码,然后重新提交跑一次jenkins

const obj = data?.['component_data']?.data
...
currentZ[`${type}`]?.length

改为

const obj = data && data['component_data'] && data.component_data.data
...
currentZ[`${type}`] && currentZ[`${type}`].length

这次跑再次提交重新跑jenkins果然是跑成功了,没有报错

但是后续又考虑到我在其他文件中也用到可不少可选链语法,为何其他地方不报错,所以我选择再次测试定位具体问题 这次我选择只是改掉其中一句代码用以测试

const obj = data && data['component_data'] && data.component_data.data

改为

const obj = data?.['component_data']?.data

然后恢复一部分代码

currentZ[`${type}`] && currentZ[`${type}`].length

改回

currentZ[`${type}`]?.length

再次提交重新跑jenkins,这次失败了,报错信息与之前的相同

结论

此时可以定位是currentZ[${type}]?.length这句代码出现了问题,可以断定jenkins在跑的时候对于']?'这种语法是会抛错的,但是具体为何本地和jenkins输出不一致,具体问题不是很清楚,因为自身对jenkins的原理也不是很熟,如果有清楚这部分原理的同学,请在评论区给出一些思路,万分感谢!