【问题标题】:Babel setup error: "ERROR in ./src/js/index.js"Babel 设置错误:“./src/js/index.js 中的错误”
【发布时间】:2018-08-30 19:46:42
【问题描述】:

更新:我解决了这个问题。查看答案。

目标:设置 Babel。

问题:我在使用 webpack 创建 bundle.js 文件时遇到错误,使用命令:npm run devscreenshot 1screenshot 2

./src/js/index.js

import num from "./test";
const x = 45;
console.log(`I imported ${num} from test.js - ${x}`);

webpack.config.js:

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
    entry: ["babel-polyfill", "./src/js/index.js"],
    output: {
        path: path.resolve(__dirname, "dist"),
    filename: "js/bundle.js"
    },
    devServer: {
    contentBase: "./dist"
    },
    plugins: [
    new HtmlWebpackPlugin({
        filename: "index.html",
        template: "./src/index.html"
    })
    ],
    module: {
    rules: [ 
        {
        test: /\.js$/, 
        exclude: /node_modules/, 
        use: { 
            loader: "babel-loader"
                }
        }
        ]
    }
};

【问题讨论】:

  • 你缺少一个模块:npm i -D @babel/core
  • 我已经安装了这个包。它显示在我的 package.json 文件中。

标签: javascript webpack babeljs


【解决方案1】:

我修复了错误。原来我需要安装新的 Babel 包。

npm install --save-dev @babel/core @babel/preset-env
npm install --save @babel/polyfill

将 webpack.config.js 中的 entry 替换为:

entry: ["@babel/polyfill", "./src/js/index.js"],

将 .babelrc 中的 presets 替换为:

{
  "presets": ["@babel/env"]
}

【讨论】:

    【解决方案2】:

    您好像错过了babel-core

    我想跑步:

    npm install --save-dev babel-core

    应该修复它

    【讨论】:

    • 我已经安装了这个包。它显示在我的 package.json 文件中。
    • 刚刚看到您在 package.json 中的其他评论。尝试删除node_modules,然后再次删除npm install
    • 或者,要确认确实安装了,打开node_modules文件夹,看看能不能看到babel-core文件夹
    • Babel-core 在 node_modules 中。
    • /.test 文件中有什么内容?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    相关资源
    最近更新 更多