Home > Software engineering >  How do I run npx commands without prefixing `npx`
How do I run npx commands without prefixing `npx`

Time:11-20

For example when I run gulp I don't have to do npx gulp.

I can omit the npx and just run gulp

How do I do this for my own package?

I've added mycommand to the package npm bin config, but I still always have to do npx mycommand for it to work.

CodePudding user response:

It depends on your operating system. On a UNIX-based OS (ie. Linux or Mac) you can use the alias command:

$ alias gulp="npx gulp"

For the rest of your terminal session, you can then run:

$ gulp

to run npx gulp. However, whenever you restart your terminal program, you'll lose the alias.

To make the alias permanent, you need to add the alias command to the appropriate start-up file (eg. .bashrc, .profile, etc.) for your OS. Simply copy/paste the exact command you used before, at the end of that file, save, and restart your terminal. You'll have the alias permanently.

Aliases in Windows are also possible, but are a bit trickier; see Aliases in Windows command prompt.

CodePudding user response:

You don't have to use npx gulp for gulp because it is installed globally

So, there's gulp.cmd, gulp.ps1 and gulp starting npx gulp somewhere in PATH, so you can run them from there

this answer is low effort, feel free to edit it

CodePudding user response:

if you install gulp globally: npm i -g gulp you will be able to just run gulp

CodePudding user response:

I would avoid answers suggesting global installs (npm install -g). Global installs guarantee that everyone working on a project has their own unique set of tools, and the reason companies end up with huge wiki pages on "how to get the project working locally".

You mention the "bin config", but perhaps you're misunderstanding that one. That's not for specifying commands that your app will use. If you were making a cli application, the bin section in package.json is where you would specify the commands exported by your project. For example, gulp exports the gulp command like:

  "bin": {
    "gulp": "./bin/gulp.js"
  },

Instead, you would add dependencies needed by your application to devDependencies. This ensures that everyone using the project will get the same version of all of the tools, such as gulp, tsc Using npx is a far better solution because you can add everything the project needs in devDependencies, and npx will use that version. For the common command line tools, an alias such as @machineghost suggests is a better way to go, but to expand on it a bit:

  • Sometimes, the name of the command is different from the name of the package. In those cases, the alias can use:
alias tsc='npx --package=typescript tsc'

Normally, when running npx, it will prompt to install if there is not a version found in the current project. This is often a good safeguard, because it reminds you to add it to the devDependencies of the project. However, if you really want it to "just work" like a global install, you can add the "yes" flag:

alias command=`npx -y gulp`

This will use the version specified in the current project if present, but install it and run it directly if not.

  • Related