预设文件不允许导出对象,Webpack打包轮播组件报错求助
Hey there, let's work through that frustrating error you're seeing when trying to build your carousel component. This issue almost always boils down to a version mismatch between Babel packages or incorrect preset configuration—here's how to fix it step by step:
1. First, Diagnose the Babel Version Conflict
The error pops up because Babel 7 completely changed how presets are structured and named. If you're using Babel 7 (the latest stable version), you need to use the new @babel/ prefixed presets. If you're still on Babel 6, stick to the old unprefixed ones—but I'd recommend upgrading to Babel 7 for better support and security.
Check your package.json to see which versions you're using:
- Look for
babel-core,babel-loader, and anybabel-preset-*packages. Ifbabel-coreis version 7.x, you need the new presets. If it's 6.x, use the old ones.
2. Fix Your Dependencies
If You're Upgrading to Babel 7 (Recommended)
Uninstall any old Babel presets and packages, then install the latest compatible ones:
# Uninstall outdated Babel 6 packages npm uninstall babel-core babel-loader babel-preset-env babel-preset-react # Install Babel 7 equivalents npm install --save-dev @babel/core babel-loader @babel/preset-env @babel/preset-react
If You're Sticking to Babel 6
Make sure all your Babel packages are on version 6.x, and your presets are the unprefixed ones:
npm install --save-dev babel-core@6 babel-loader@7 babel-preset-env@1 babel-preset-react@6
(Note: Babel 6 is no longer maintained, so this is only a temporary fix if you can't upgrade right now.)
3. Update Your Babel Configuration
Your preset references in either .babelrc, babel.config.js, or your webpack.config.js need to match the package names you installed.
Example .babelrc for Babel 7:
{ "presets": [ "@babel/preset-env", "@babel/preset-react" // Add this if you're using React for your carousel ] }
Example Webpack Config Babel Rule (Babel 7):
If you're defining Babel options directly in webpack.config.js, update the presets array:
module.exports = { // ... your other webpack settings (entry, output, etc.) module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: "babel-loader", options: { presets: ["@babel/preset-env", "@babel/preset-react"] } } } ] } };
4. Eliminate Global vs Local Package Conflicts
Sometimes the error happens because you have a global installation of Babel that conflicts with your project's local version. To fix this:
- Uninstall any global Babel packages:
npm uninstall -g babel-core babel-cli - Always run Babel/webpack commands using your project's local dependencies (use
npx webpackor define scripts inpackage.jsonlike"build": "webpack"and runnpm run build).
5. Clean Up and Reinstall Dependencies
If you're still seeing the error after the above steps, try wiping your node_modules and lock file to start fresh:
rm -rf node_modules package-lock.json npm install
That should resolve the "Preset files are not allowed to export objects" error and let you build your block.build.js successfully.
内容的提问来源于stack exchange,提问作者sonia maklouf

