2

I'm trying to run a simple node.js file which needs the @babel/preset-env preset. The moment I run the js file, I get a message saying

Requires Babel “7.0.0-0” but was loaded with “6.26.3”

To replicate the issue, please try the following in a new folder: 1. Run the following commands

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
{
"presets": ["@babel/preset-env"],
"plugins": []
}
  1. Create a sample emp.jsx with the following
import React from "react";
class CommentBox extends React.Component {}
  1. Create a parse.js file with the following
require('babel-register')({presets: ['env', 'react']});
let Emp = require('./emp.jsx');

Now run the parse.js file by running

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

Arun
  • 3,036
  • 3
  • 35
  • 57
  • you should install and use `@babel/register` instead – William Chong Jan 12 '19 at 07:16
  • Thanks @WilliamChong. Tried that, but no luck. I still get the same error. When do you suggest I install @babel/register? I installed it after installing @babel/core@^7.2.2 – Arun Jan 12 '19 at 07:25

2 Answers2

1

followed your instructions and using @babel/register instead with this package.json run with no issues

tasted on
node : v8.11.2
yarn : 1.12.3

paese.json

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

packge.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"
  }
}
Naor Tedgi
  • 5,204
  • 3
  • 21
  • 48
  • 1
    Thanks @Naor, doesn't work for me. This is the batch file I'm running call npm init call npm install @babel/register call npm install @babel/core@^7.2.2 call npm install @babel/preset-env node parse.js Get the same error. Also have made a small edit to the parse.js file above. Please have a look. – Arun Jan 12 '19 at 10:59
0

Found the problem. The .babelrc file that contained a reference to @babel/preset-env. Removed it and the js file compiled just fine.

Arun
  • 3,036
  • 3
  • 35
  • 57