0

I am trying to use babel-node to run files in my node project.

I am able to get them running if I set up an npm script, such as:

"scripts": {
    "start": "babel-node src/index.js"
  },

However, if I try to run it via the cli, I get the error: zsh: command not found: babel-node.

I've already tried installing babel-node both globally, and as a dev-dependency, via:

npm i -g @babel/node @babel/cli

But this hasn't worked for me.

Does anyone know why the shell command doesn't work?

713sean
  • 313
  • 11

1 Answers1

1

per https://babeljs.io/docs/babel-node

Compile and run test.js.

Shell

npx babel-node test

so you should update your script to

"scripts": {
    "start": "npx babel-node src/index.js"
  },

And you should be able to lose the /index.js part, simplifying to

"scripts": {
    "start": "npx babel-node src"
  },
Joe Lissner
  • 2,181
  • 1
  • 15
  • 21
  • Thanks, `npx babel-node src` works. But why do we use `npx` if I have installed the package to my `node_modules`? I figure I should be able to run it with something like `babel-node `. – 713sean Mar 27 '23 at 19:20
  • 1
    You can learn more about `npx` here, https://www.npmjs.com/package/npx, but basically node-babel is not in your path and `npm` has updated how they look for binaries, which necessitates the need to use `npx` – Joe Lissner Mar 27 '23 at 19:23
  • Thanks, I marked your answer and appreciate all the extra info. One last question, in my script, I'm able to use `"start": "babel-node "` as opposed to `"start": "npx babel-node "`, why is this? Since I cannot use `babel-node` in the CLI. – 713sean Mar 27 '23 at 19:29
  • 1
    I can't find the exact documentation off hand, but `npm` auto prefixes it's scripts with `npx` when it's not present. I honestly misread your question at first and was really surprised it was needed and was trying to reproduce, but that's what I get for not reading well :P – Joe Lissner Mar 27 '23 at 20:35