Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 417
joedotnot Avatar asked Jul 26 '18 02:07

joedotnot


People also ask

How do I run babel script?

Simply add a "scripts" field to your package. json and put the babel command inside there as build . This will run Babel the same way as before and the output will be present in lib directory, only now we are using a local copy. Alternatively, you can reference the babel cli inside of node_modules .

Can I use babel-node in production?

You should not be using babel-node in production. It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly.

How do I run a Babel-node script in VS Code?

There's a couple of different ways to run a script with babel-node, just like with node itself. Passing it the name of a script will invoke the script just like it would with node. You can also add the --inspect flag and in VS Code or using chrome://inspect, you can debug your command line script.

How do I compile a Babel file from the command line?

Babel comes with a built-in CLI which can be used to compile files from the command line. In addition, various entry point scripts live in the top-level package at @babel/cli/bin. There is a shell-executable utility script, babel-external-helpers.js, and the main Babel cli script, babel.js.

Is there a way to use @Babel/node with Node JS?

That's where tools like @babel/node come in. @babel/node allows you to "transpile" code from Typescript, or "ES Next" JavaScript with modules, or whatever to Javascript that can run in nodejs, and it does this at runtime as opposed to pre-transpiling. Aka you can do stuff like this, and run Typescript files without "pre" compiling them.

How do I run a node Program in shell script?

The usual way to run a Node.js program is to run the globally available node command (once you install Node.js) and pass the name of the file you want to execute. If your main Node.js application file is app.js, you can call it by typing: Above, you are explicitly telling the shell to run your script with node.


1 Answers

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
like image 155
Viktor Vlasenko Avatar answered Sep 20 '22 12:09

Viktor Vlasenko