2

Currently, I'm working on a personal project and I'd like to create an alias for some commands that I have to run... Similar to:

jest ...

node ...

tsc ...

I think that for my project would be so helpful and cool to have something like

foo ...

I'm working with Node, Typescript, and JS.

I took a look on the internet and read saw some people teaching how to create some alias I tried and it works :) I already have the alias working on my local machine cause I added the alias on the .bashrc file.

alias foo = command...

However, I also tried to put it on my package.json scripts section, like:

"scripts": { "runFoo": "foo",

But when I run npm run runFoo it says that "foo" is not recognized... Are any way to do that? How the tools like jest do that?

I would be thankful for any direction about what to study for that.

Extra: There is any way to run all the .js from a folder by using any node command without knowing the name of the files? Like:

node *.js

It can help while I don't figure out how to do the alias...

Edit: What I want to do is: https://developer.okta.com/blog/2019/06/18/command-line-app-with-nodejs

The answer helped me to find this post, and following it, it worked here.

  • If the command has been added to the scripts object in package.json, you can run it with `npm run `. – ericksonb Jan 27 '23 at 02:02
  • Yeah! But it is not it, I have the script "runFoo", It runs the "foo" command... "foo" is an alias to some verbose command that I wrote. When I do npm run runFoo it says that "foo" is not a valid command... While if I run it on my bash terminal it works – Gabriel Dias de Oliveira Jan 27 '23 at 02:05
  • I think people typically don't modify .bashrc to work on node projects (or projects using other languages), because they can't be synced and must be manually edited every time you set up your project. – Daniel Cheung Jan 27 '23 at 05:09
  • haha, it would be so cool. But looks like we found a possible solution! :D – Gabriel Dias de Oliveira Jan 27 '23 at 13:15

1 Answers1

2

it says that "foo" is not recognized

That is because shell aliases do not work in npm scripts (they are only valid when directly called in said shell), see e.g. How can I pipe to a bash alias from an npm script?

Aliases are meant to reduce typing, and are not automatically shared across environments, on the contrary of npm scripts which are committed with package.json. Hence if you try running that script in another environment, the latter may not know that alias (or worse, use it for something else!).


How the tools like jest do that?

They do not... Their command is not an alias, but an actual executable binary or script.

When installed as dependencies, you will find links to their executable in node_modules/.bin/ folder, see What is the purpose of .bin folder in node_modules?

You can easily create such executable scripts, even written in JavaScript to be executed by a Node.js engine, see e.g. Appropriate hashbang for Node.js scripts

ghybs
  • 47,565
  • 6
  • 74
  • 99
  • 1
    Thanks a lot! It was exactly what I was looking for... I did some tests here, by searching the things that you describe and found that link https://developer.okta.com/blog/2019/06/18/command-line-app-with-nodejs This is exactly what I want to do :D will apply it to my project! – Gabriel Dias de Oliveira Jan 27 '23 at 13:11