how to run node / babel script directly in command line?
Asked Answered
F

7

20

My package.json looks like:

{
  "name": "99-nodetest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "babel-node --presets env app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "latest"
  }
}

The js script i want to run is app.js. I cannot run it directly using node app.js because app.js contains new language syntax.

Thus i have to run it through babel, using npm start, as per the start script defined above. No issues here.

My question is how to run the cmd directly in the command line, can it be done? something similar to:

npm run babel-node --presets env app.js

Funk answered 26/7, 2018 at 2:45 Comment(1)
just FYI (and to make the link): the above question is related to my question hereSchram
D
23

node ./node_modules/babel-cli/bin/babel-node.js --presets env app.js

Donne answered 26/7, 2018 at 2:52 Comment(3)
node ./node_modules/babel-cli/bin/babel-node.js --presets env app.js . NB: its actually under babel-cli. if you change your answer, i wiil accept itFunk
I ended up staying with Babel 6, instead of upgrading to Babel 7, just so I didn't have to deal with this regression.Bougie
For anyone reading this in 2020+, with Babel v7 things have changed quite a bit ! You may need to look elsewhere rather than this accepted answer, which works with Babel 6 !Funk
N
28

You can execute npm package binaries with npx.

Because Babel 7 always resolves plugins and presets relative to local project folder, you will have to install @babel/preset-env locally into the project.

npm i -D @babel/preset-env

After that the babel-node can be run with npx without installation into the project:

npx -p @babel/core -p @babel/node babel-node --presets @babel/preset-env app.js

If you install @babel/node into the project, npx will prefer project-local version.


In case of Babel 6 the command below can be used:

npx babel-node --presets env app.js
Njord answered 26/7, 2018 at 6:15 Comment(3)
In 2019: npx babel-node --presets @babel/preset-env app.jsInventive
@ArneJenssen Thank you. Updated my answer. With Babel 7 it is actually much harder than it was with Babel 6, unfortunatelyNjord
But How to give options for @babel/preset-env in the command. Like "modules": false can be given in babel.config.json. But how to give in command line?Evertor
D
23

node ./node_modules/babel-cli/bin/babel-node.js --presets env app.js

Donne answered 26/7, 2018 at 2:52 Comment(3)
node ./node_modules/babel-cli/bin/babel-node.js --presets env app.js . NB: its actually under babel-cli. if you change your answer, i wiil accept itFunk
I ended up staying with Babel 6, instead of upgrading to Babel 7, just so I didn't have to deal with this regression.Bougie
For anyone reading this in 2020+, with Babel v7 things have changed quite a bit ! You may need to look elsewhere rather than this accepted answer, which works with Babel 6 !Funk
S
14

Great gugley mugleys! This was way harder than it should have been.

See here for docs. TLDR;

Babel > version 7.0 has to go in your package.json to run from command line.

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

npx babel-node --presets @babel/preset-env imports/test.js 
Saiff answered 18/2, 2019 at 21:39 Comment(1)
Excellent, thanks! "By default, npx will check whether <command> exists in $PATH, or in the local project binaries, and execute that." npmjs.com/package/npxNorri
W
8

Install @babe/node globally-

npm i -g @babel/node

then babel-node command becomes available in your terminal. So, you can run -

babel-node --presets env app.js

Btw, it should be used in dev environment only, never recommended for production as it's unnecessarily heavy with high memory usage.

Waspish answered 26/7, 2018 at 2:55 Comment(1)
Best solution if you just want to run a quick test and not change the project.Annal
L
2

Babel node has a bin registered so an executable is generated on install inside the node_modules/.bin directory.

You may run it simply by typing.

node_modules/.bin/babel-node --presets env app.js

Which accomplishes the same thing as the longer node or the alternate npx versions.

Leuco answered 24/1, 2019 at 16:28 Comment(0)
R
2

in babel 7 you can run this:

npx babel app.js

Ruggiero answered 22/4, 2020 at 21:12 Comment(1)
Will npx (unlike Rodrigo's V6 answer above) pull all the required node_modules into cache? (Thus doing a lot more than on-the-fly-transpile+execution of local source code? Because I'd rather limit myself to that). — or is this not the case, when a local file (app.js) is given as parameter?Schram
L
-1

You can run the app.js file from node by telling it about babel-node first:

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

with the following .babelrc file at the root project

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

Loree answered 14/1, 2020 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.