0

I have a simple tests.js file that I wish to follow up with a source map file. I have tried initiating the dependencies yet the prompt shows the error. If anyone could specify the problem and the solution would be grateful ^^

tests.js

var add = (...arr) => {
    return arr.reduce((sum, el) =>{
        return sum+el;
    }, 0)
}
console.log(add(1,2,3));

I've tried these commands at first

npm install -g babel-cli

npm install babel-preset-es2015

babel tests.js --out-file tests.dist.js --source-maps --presets=es2015

But received the same error. I have followed another solution from the community but it still didn't work. The solution was to remove node_modules and reinitiate the dependencies.

npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node

and add "start": "nodemon --exec babel-node index.js", in the dependencies. I checked the node_modules and it had the existence of these files too.

node_modules/.bin/babel-node

node_modules/.bin/babel-node.cmd - windows only

node_modules/@babel/node/bin/babel-node.js

The solution I followed Still, I couldn't figure out how to solve this issue. This is my first time working with node and babel. My node version is v16.13.1

Edited

Folder Structure

Y:.
|   index.html
|   package-lock.json
|   package.json
|   tests.js
|   tree.txt
|   
\---node_modules
    |   .package-lock.json
    |   
    +---.bin
    |       babel
.....

It's huge!

package.json

{
  "dependencies": {
    "@babel/cli": "^7.16.8",
    "@babel/core": "^7.16.12",
    "@babel/preset-env": "^7.16.11"
  }
}

.babelrc

{
    "presents": [
        "@babel/preset-env"
    ]
}

./node_modules/@babel/cli/bin/babel.js

require("../lib/babel");

After using bash for the command -

./node_modules/@babel/cli/bin/babel.js example.js --out-file main.dist.js

The error

Error: Unknown option: .presents. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.
    at throwUnknownError (Y:\babel work\node_modules\@babel\core\lib\config\validation\options.js:133:27)
    at Y:\babel work\node_modules\@babel\core\lib\config\validation\options.js:118:5
    at Array.forEach (<anonymous>)
    at validateNested (Y:\babel work\node_modules\@babel\core\lib\config\validation\options.js:94:21)
    at validate (Y:\babel work\node_modules\@babel\core\lib\config\validation\options.js:85:10)
    at Y:\babel work\node_modules\@babel\core\lib\config\config-chain.js:209:34
    at cachedFunction (Y:\babel work\node_modules\@babel\core\lib\config\caching.js:60:27)
    at cachedFunction.next (<anonymous>)
    at evaluateSync (Y:\babel work\node_modules\gensync\index.js:251:28)
    at sync (Y:\babel work\node_modules\gensync\index.js:89:14) {
  code: 'BABEL_UNKNOWN_OPTION'
}
8
  • did you set .babelrc file with some configuration like this { "presets": [ "@babel/preset-env" ] } Commented Feb 1, 2022 at 8:44
  • Can you share your folder structure?
    – cooskun
    Commented Feb 1, 2022 at 8:54
  • @cooskun Hi, I have added some portion of the folder structure. It has .bin and @babel and a lot of stuff in node_modules.
    – Era
    Commented Feb 1, 2022 at 9:37
  • @ullaskunder Hi, I tried the solution you posted. added the .babelrc file but whenever I go for /babel.js --version it shows require("../lib/babel"); in the babel.js file. How to go forth?
    – Era
    Commented Feb 1, 2022 at 9:39
  • 1
    @ullaskunder Thank you, so much! I sorted all the issues now Thanks to you!
    – Era
    Commented Feb 1, 2022 at 11:27

1 Answer 1

2

Hyy, [update]

installing babel locally

  • npm i @babel/core @babel/cli @babel/preset-env

  • Inside package.json add npm script

  "scripts": {
    "start-babel": "babel example.js --out-file main.dist.js"
  },
  • You need .babelrc file for all you configuration
// basic need
{
    "presets": [
        "@babel/preset-env"
    ]
}
  • Run script by npm run start-babel this will create main.dist.js js transpiled file

  • You need .babelrc file with some configuration

  • if you have installed @bable/core @babel/cli @bable/preset-env locally

    • Then you have to use the path of of babel.js of node_modules like this

enter image description here

  • if you installed it globally using -g then you don't need a path just use babel

Not the answer you're looking for? Browse other questions tagged or ask your own question.