配置corejs版本问题解决

博主在使用自己编写的脚手架工具时,直接使用 @babel/core 执行代码报错,参考相关链接后仍未解决。修改 corejs 版本也报错,同样参考了相关链接。最后给出了正确的配置方式所在文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己写的脚手架工具 npm i -g xd-es-cli
报错 =>
直接使用@babel/core

    "@babel/core": "^7.12.3",

执行如下代码报错 =>

let {name, age = 18} = {
    name: "xxx",
    age: 28
}
console.log(name, age)

报错 =>

i 「wds」: Project is running at http://localhost:8080/
i 「wds」: webpack output is served from undefined
i 「wds」: Content not from webpack is served from H:\ld\task\code\ECMScript\mk02\es-demo

WARNING: We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option.

You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands:

  npm install --save core-js@2    npm install --save core-js@3
  yarn add core-js@2              yarn add core-js@3

(node:17604) [DEP_WEBPACK_COMPILATION_ASSETS] DeprecationWarning: Compilation.assets will be frozen in future, all modifications are deprecated.
BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
        Do changes to assets earlier, e. g. in Compilation.hooks.processAssets.
        Make sure to select an appropriate stage from Compilation.PROCESS_ASSETS_STAGE_*.
(Use `node --trace-deprecation ...` to show where the warning was created)
× 「wdm」: assets by path static/ 15.1 KiB
  asset static/favicon.ico 13.3 KiB [emitted] [from: static/favicon.ico] [copied]
  asset static/1-1.js 1.07 KiB [emitted] [from: static/1-1.js] [copied]
  asset static/1-2.js 750 bytes [emitted] [from: static/1-2.js] [copied]
asset index.js 896 KiB [emitted] (name: main)
asset index.html 310 bytes [emitted]
runtime modules 704 bytes 4 modules
modules by path ./node_modules/ 330 KiB
  modules by path ./node_modules/webpack-dev-server/client/ 20.9 KiB 10 modules
  modules by path ./node_modules/html-entities/lib/*.js 57.9 KiB 4 modules
  modules by path ./node_modules/webpack/hot/ 1.58 KiB 3 modules
  modules by path ./node_modules/url/ 37.4 KiB 3 modules
  modules by path ./node_modules/querystring/*.js 4.51 KiB
    ./node_modules/querystring/index.js 127 bytes [built] [code generated]
    ./node_modules/querystring/decode.js 2.34 KiB [built] [code generated]
    ./node_modules/querystring/encode.js 2.04 KiB [built] [code generated]
modules by path ./src/*.js 1.34 KiB
  ./src/index.js 98 bytes [built] [code generated]
  ./src/1-3.js 1.25 KiB [built] [code generated]

ERROR in ./src/1-3.js 1:0-43
Module not found: Error: Can't resolve 'core-js/modules/es6.function.name' in 'H:\ld\task\code\ECMScript\mk02\es-demo\src'
 @ ./src/index.js 4:0-15

webpack 5.4.0 compiled with 1 error in 1888 ms
i 「wdm」: Failed to compile.

参考 => https://github.com/nuxt/nuxt.js/issues/5287
图片描述
修改corejs版本

"plugin": [
  ["@babel/plugin-transform-runtime", {
  "corejs":2
}]
]

报错

i 「wds」: Project is running at http://localhost:8080/
i 「wds」: webpack output is served from undefined
i 「wds」: Content not from webpack is served from H:\ld\task\code\ECMScript\mk02\es-demo

WARNING: We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option.

You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands:

  npm install --save core-js@2    npm install --save core-js@3
  yarn add core-js@2              yarn add core-js@3

(node:9088) [DEP_WEBPACK_COMPILATION_ASSETS] DeprecationWarning: Compilation.assets will be frozen in future, all modifications are deprecated.
BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
        Do changes to assets earlier, e. g. in Compilation.hooks.processAssets.
        Make sure to select an appropriate stage from Compilation.PROCESS_ASSETS_STAGE_*.
(Use `node --trace-deprecation ...` to show where the warning was created)
× 「wdm」: assets by path static/ 15.1 KiB
  asset static/favicon.ico 13.3 KiB [emitted] [from: static/favicon.ico] [copied]
  asset static/1-1.js 1.07 KiB [emitted] [from: static/1-1.js] [copied]
  asset static/1-2.js 750 bytes [emitted] [from: static/1-2.js] [copied]
asset index.js 891 KiB [emitted] (name: main)
asset index.html 310 bytes [emitted]
runtime modules 430 bytes 3 modules
cacheable modules 330 KiB
  modules by path ./node_modules/webpack-dev-server/client/ 20.9 KiB 10 modules
  modules by path ./node_modules/html-entities/lib/*.js 57.9 KiB 4 modules
  modules by path ./node_modules/url/ 37.4 KiB 3 modules
  modules by path ./node_modules/querystring/*.js 4.51 KiB
    ./node_modules/querystring/index.js 127 bytes [built] [code generated]
    ./node_modules/querystring/decode.js 2.34 KiB [built] [code generated]
    ./node_modules/querystring/encode.js 2.04 KiB [built] [code generated]
  modules by path ./node_modules/webpack/hot/*.js 1.42 KiB
    ./node_modules/webpack/hot/emitter.js 75 bytes [built] [code generated]
    ./node_modules/webpack/hot/log.js 1.34 KiB [built] [code generated]
./node_modules/webpack/hot/ sync nonrecursive ^\.\/log$ 170 bytes [built] [code generated]

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: [BABEL] H:\ld\task\code\ECMScript\mk02\es-demo\src\index.js: .corejs is not a valid Plugin property
- Maybe you meant to use
"plugin": [
  ["@babel/plugin-transform-runtime", {
  "corejs": 3
}]
]
To be a valid plugin, its name and options should be wrapped in a pair of brackets
    at H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\@babel\core\lib\config\validation\plugins.js:65:42
    at Array.forEach (<anonymous>)
    at validatePluginObject (H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\@babel\core\lib\config\validation\plugins.js:54:20)
    at H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\@babel\core\lib\config\full.js:256:55
    at Generator.next (<anonymous>)
    at Function.<anonymous> (H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\@babel\core\lib\gensync-utils\async.js:26:3)
    at Generator.next (<anonymous>)
    at step (H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\gensync\index.js:261:32)
    at evaluateAsync (H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\gensync\index.js:291:5)
    at Function.errback (H:\ld\task\code\ECMScript\mk02\es-demo\node_modules\gensync\index.js:113:7)

webpack 5.4.0 compiled with 1 error in 1773 ms

参考 => https://github.com/babel/babel/issues/8451
图片描述
正确配置方式 =>
es-demo\build\webpack.base.config.js

    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: [
                        ['@babel/preset-env', {
                            "useBuiltIns": "usage"
                        }]
                    ],
                    plugins: [[
                        '@babel/plugin-transform-runtime',
                        {
                            corejs: { version: 2 } // 指定 runtime-corejs 的版本,目前有 2 3 两个版本
                        }
                    ]]
                }
            }
        }]
    },
<think>好的,我需要帮助用户解决如何升级core.js到最新版本问题。首先,我得回顾用户提供的引用内容,看看有没有相关的信息。引用[1]提到core.js用于处理IE的兼容性问题,引用[2]提到引入xlsx.core.min.js,而引用[3]详细解释了core.js的作用,即提供polyfill让旧浏览器支持新API。不过用户的问题是关于升级core.js,所以需要从常规的npm包管理角度来考虑。 用户可能正在使用npm或者yarn这样的包管理工具。升级core.js通常是通过更新package.json中的版本号,然后运行安装命令。首先需要确认当前项目使用的core.js版本,可以通过查看package.json或者运行npm list core-js。然后,查找最新的core.js版本号,可以使用npm info core-js命令。接着,更新package.json中的版本号到最新,并运行npm install或yarn install来安装新版本。升级后需要进行测试,确保没有兼容性问题,特别是如果项目依赖旧版core.js的特性的话。 另外,用户可能需要注意core.js版本变化可能带来的API变化或配置调整,比如是否需要更改polyfill的引入方式。如果项目使用babel等工具,可能还需要检查相关的babel配置是否兼容新版的core.js。此外,用户提到的xlsx.core.min.js可能是一个特定的库,需要确认是否与core.js的最新版本有依赖关系,避免冲突。 最后,提醒用户在升级后进行全面的测试,尤其是在不同的浏览器上验证兼容性,确保升级后的core.js能够正常工作,不会引入新的问题。如果遇到问题,可以查看core.js的官方文档或社区支持寻求帮助。</think>### 如何升级 core.js 到最新版本? #### 步骤说明 1. **确认当前版本** 在项目的 `package.json` 文件中查找 `core-js` 的版本号。例如: ```json "dependencies": { "core-js": "^3.20.0" } ``` 也可以通过命令行查看: ```bash npm list core-js # 使用 npm yarn list core-js # 使用 yarn ``` 2. **查找最新版本** 访问 [core-js 的 npm 页面](https://www.npmjs.com/package/core-js) 或通过命令行获取最新版本信息: ```bash npm info core-js version # 输出最新版本号(如 3.33.0) ``` 3. **更新版本** - **手动修改**:在 `package.json` 中将 `core-js` 的版本号改为最新(例如 `^3.33.0`)。 - **命令行自动更新**(推荐): ```bash npm install core-js@latest # npm 更新到最新版本 yarn upgrade core-js@latest # yarn 更新到最新版本 ``` 4. **验证安装** 更新后再次运行 `npm list core-js` 或 `yarn list core-js`,确认版本已变更。 5. **测试兼容性** - 运行项目测试用例,检查是否存在因版本升级导致的语法或功能问题- 特别关注 `Promise`、`Set` 等 ES6+ API 的兼容性表现[^3]。 #### 注意事项 - **Polyfill 调整**:新版 `core-js` 可能调整了部分 polyfill 的实现方式,需检查项目中是否依赖特定行为。 - **Babel 配置**:如果使用 Babel,确保 `@babel/preset-env` 的 `corejs` 配置与新版一致: ```json "presets": [ ["@babel/preset-env", { "useBuiltIns": "usage", "corejs": 3.33 // 与安装版本一致 }] ] ``` - **浏览器兼容性**:升级后需重新验证目标浏览器(如 IE)的支持情况[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值