解決 Webpack 跑 bundle 發出 Unexpected token import 的 error

Ryan Hsu
Ryan Hsu
Aug 29, 2017 · 1 min read

情境

webpack.config.js 使用 ES6 語法,但在跑

$ npm run webpack --config ./webpack.config.js

卻告訴我,Node 不認得 ES6 webpack.config.js 中 import 語法?

解決方法

請把 webpack.config.js 「檔案名稱」改為webpack.config.babel.js

這麼一來在跑 webpack config 的時候就會以 ES6 模式下去跑了!

官方網站好像也沒有把這點特別寫出來,但我在 Stackoverflow 有找到相關的議題:How can I use ES6 in webpack.config.js?

以上

記錄一下,這個連我自己都常常忘記,希望對你也有幫助!

)
Ryan Hsu

Written by

Ryan Hsu

Front-end developer

Its Ok to Make Mistakes

分享UI UX 設計、前端開發、數位行銷所遇到的雷點 。It’s not how we make mistakes, but how we correct them that defines us.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade