简体   繁体   English

无法使用 @babel/preset-env 运行 node.js 文件

[英]Unable to run a node.js file with @babel/preset-env

I'm trying to run a simple node.js file which needs the @babel/preset-env preset.我正在尝试运行一个需要 @babel/preset-env 预设的简单 node.js 文件。 The moment I run the js file, I get a message saying我运行 js 文件的那一刻,我收到一条消息说

Requires Babel “7.0.0-0” but was loaded with “6.26.3”需要 Babel “7.0.0-0” 但加载了 “6.26.3”

To replicate the issue, please try the following in a new folder: 1. Run the following commands要复制该问题,请在新文件夹中尝试以下操作: 1. 运行以下命令

npm init
npm install @babel/register
npm install @babel/core@^7.2.2
npm install @babel/preset-env
  1. Create a .babelrc file with the following使用以下内容创建 .babelrc 文件
{ "presets": ["@babel/preset-env"], "plugins": [] }
  1. Create a sample emp.jsx with the following使用以下内容创建示例 emp.jsx
 import React from "react"; class CommentBox extends React.Component {}
  1. Create a parse.js file with the following使用以下内容创建 parse.js 文件
require('babel-register')({presets: ['env', 'react']}); let Emp = require('./emp.jsx');

Now run the parse.js file by running现在通过运行运行 parse.js 文件

node parse.js

You should see the error mentioned above.您应该会看到上面提到的错误。 I have been trying to fix with for a long time now.我已经尝试修复很长时间了。 Please help.请帮忙。

Many Thanks非常感谢

followed your instructions and using @babel/register instead with this package.json run with no issues按照您的指示并使用 @babel/register 代替这个 package.json 运行没有问题

tasted on
node : v8.11.2
yarn : 1.12.3

paese.json paese.json

require('@babel/register')({});
let Emp = require('./emp.jsx');
console.log(Emp)

packge.json包.json

{
  "name": "sof",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/preset-env": "^7.2.3",
    "@babel/register": "^7.0.0"
  },
  "dependencies": {
    "react": "^16.7.0"
  }
}

Found the problem.发现问题了。 The .babelrc file that contained a reference to @babel/preset-env.包含对@babel/preset-env 的引用的 .babelrc 文件。 Removed it and the js file compiled just fine.删除它并且js文件编译得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在小型节点项目上使用带有 preset-env 的 babel,无法将 import/export 语句转换为 commonjs - Using babel with preset-env on a small node project, unable to convert import/export statements to commonjs 在节点应用程序中使用 babel/preset-env 启用扩展运算符 - Enable spread operator using babel/preset-env in node application 尝试安装 @babel/preset-env 时出错 - error trying to install @babel/preset-env Babel preset-env 未加载节点目标的顶级等待语法 - Babel preset-env not loading top level await syntax for node target 如何在package.json文件中配置babel / preset-env? - How do I configure my babel/preset-env in my package.json file? 模块构建失败找不到模块'@babel/preset-env' - Module build failed Cannot find module '@babel/preset-env' @babel/preset-env 版本 7 未转换 null-coalescing 运算符 - @babel/preset-env version 7 not transforming the null-coalescing operator 尝试运行 vue 项目但错误:找不到模块'@vue/cli-plugin-babel' 我安装了依赖项 npm install @babel/core @babel/preset-env - Trying to run vue project but Error: Cannot find module '@vue/cli-plugin-babel' I installed the dependency npm install @babel/core @babel/preset-env Babel Preset ENV问题 - Babel Preset ENV issues 为什么我在使用preset-env节点时无法解析nodejs默认模块? - Why can't I resolve nodejs default modules when using preset-env node?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM