First type NPM config list on the terminal to find the address to install the global package
npm config list
Copy the code
After a successful copy, remove a \ when copying to search
Because \ means network directory,\ means local directory
Let’s say my global package is in this directory
C:\Users\sandy\AppData\Roaming\npm
Copy the code
After going to the node_modules folder, we can see the global package installed locally
Did we input any NPM, NRM,yarn instructions to feedback the result to us
So how do we customize the commands?
Do this in package.json in the custom local package
Add the bin attribute and let’s do a simple configuration
{" name ":" gt999 ", "version" : "1.0.0", "description" : ""," main ":" index. Js ", "scripts" : {" test ", "echo \" Error: no test specified\" && exit 1" }, "bin": { "gt": "index.js" }, "keywords": [], "author": "", "license": "ISC" }Copy the code
What does this bin attribute mean?
This means that we have a custom directive called gt. When we type this directive, we will look up the index.js file and execute the code inside
Now let’s try the GT directive
Is it obvious that it’s wrong? Why is it wrong? Because we haven’t imported the global package yet
How to import?
Enter NPM link at the end of the custom local package
npm link
Copy the code
Let’s try it out after we import it and see if it works
Is it still no good? Why not?
Because the system doesn’t know under what circumstances to execute this file
This file should be executed in the Node environment
Do we need to execute it in the Node environment
How do I get this file to execute in the Node environment
Just add #! To the top of the file that you want to execute. /usr/bin/env node
#! /usr/bin/env node
Copy the code
Import global package again later (NPM link)
And then try again to see if we can execute our custom instructions
Now there is no problem
Let’s try uploading the local package and installing the global package to see if it works
Well, this is pretty easy, and we talked about it in the last video, but it won’t and you can look at the article in the last video
Juejin. Cn/post / 690529…
Just upload it
Then install the package globally
npm i gt999 -g
Copy the code
And finally, let’s test it out
Ah, is there no problem