最近工做比較忙,有一段時間沒有寫前端玩了。今天試着搭一個項目,發現各類坑,之前用起來很是好的配置文件各類報錯。排查後發現原來babel
升級了一個大版本,已經到7.X
了,這裏我總結一下升級過程當中踩到的坑。前端
Error: Cannot find module '@babel/core' babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'. at Function.Module._resolveFilename (module.js:547:15) at Function.Module._load (module.js:474:25) at Module.require (module.js:596:17) at require (internal/module.js:11:18) ....
沒找到@babel/core
,須要把babel-core
卸載掉,重新安裝@babel/core
npm un babel-core
npm i -D @babel/core
node
ERROR in ./src/index.jsx Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Plugin/Preset files are not allowed to export objects, only functions. ...
將babel-preset-*
卸載,從新安裝@babel/preset-*
,而且修改 .babelrc
中的 presets
react
好比個人webpack
npm: - babel-preset-env + @babel/preset-env - babel-preset-react + @babel/preset-react - babel-preset-stage-0 .babelrc: - "presets": ["react", "env", "stage-0", "mobx"] + "presets": ["@babel/preset-react", "@babel/preset-env", "mobx"]
除了上述的preset
,我還用了babel-preset-mobx
可是沒找到 @babel/preset-mobx
,從babel-preset-mobx git提交日誌上看,做者已經支持了最新的babel
。在以後的測試中,發現mobx
的功能也能正常使用。
另外,stage-*已棄用git
ERROR in ./src/index.jsx Module build failed (from ./node_modules/babel-loader/lib/index.js): TypeError: this.setDynamic is not a function at PluginPass.pre ...
此次是插件了,同樣把babel-plugin-*
卸載,從新安裝@babel/plugin-*
而後修改.babelrc
文件
具體的包名能夠在 npm倉庫 裏找github
.babelrc:
web
{ "presets": ["@babel/preset-env", "@babel/preset-react", "mobx"], "plugins": [ "@babel/plugin-proposal-object-rest-spread", "@babel/plugin-transform-runtime" ] }
package.json:
npm
"devDependencies": { "@babel/core": "^7.1.0", "@babel/plugin-proposal-object-rest-spread": "^7.0.0", "@babel/plugin-transform-runtime": "^7.1.0", "@babel/preset-env": "^7.1.0", "@babel/preset-react": "^7.0.0", "babel-loader": "^8.0.2" "babel-preset-mobx": "^2.0.0", ... }, "dependencies": { "@babel/runtime": "^7.0.0", ... }
此次升級,功能上有什麼變化我就不在這裏寫了,你們能夠自行搜索json
總的來講,babel
捨棄了之前的 babel-*-*
的命名方式,改爲了@babel/*-*
修改依賴和.babelrc
文件後就能正常啓動項目了。webpack
不用修改(除非你是webpack 3.X
升webpack 4.X
)babel
上面的只是我遇到的問題,若是還有其餘問題,能夠參考資料 升級指南 Upgrade to Babel 7