preface
As a server language, Node.js has many apis for low-level operations, such as file writing, HTTP server and other modules. At the same time, the syntax and JS are almost always there, so as the front end, we can use these things to help solve some practical problems in development.
Front knowledge
- Every time the shell starts a new session, the shell generates environment variables, which are variables that define system attributes. Run the env command to view environment variables
- Many programs will use this variable, for example NVM will use NVM_NODEJS_ORG_MIRROR=nodejs.org/dist
As the proxy server address, download node from the source of the image to increase the speed of installation
-
Where is the global package installed in NPM root -g
-
Argv [0] is the path of the node itself, process.argv[1] is the path of the file, and process.argv[1] is the path of the file. Process.argv.slice (2) is the parameter we enter
How do I create the Node tool
- Start by creating a file
mkdir test
cd test
Go to the folder- Initialize our project
npm init
All the way back to the car, there should be onepackage.jsonThe file - Open the Test folder with one of your favorite development tools
This is what you should seeAt this point, the project is created
How to Configure commands
For node projects, the module export entry file is specified by the main field of package.json or by the bin field of package.json for tools to be installed on the command line.
The package.json file you first create should look like this (if you press Enter all the way)
We should then configure it with a bin field, as shown below
What this means is that we create a test-abc command, and when you type this command, execute the index.js file in the same directory.
So now we need to write our code logic under the index.js file
Formal development tools
Create the index.js file. The current file structure should be:
In index.js write:
#! /usr/bin/env node
console.log("test-abc");
Copy the code
The first line is the script declaration file. Anything with shebang line(#!) The opening file is the executable script, where the shebang line specifies what (interpreter) is used to interpret the execution script
For a common Python script, you can see that the first line looks like this: #! /usr/bin/env python
For Node.js, shebang line usually reads: #! /usr/bin/env node
Shebang specifies the script interpreter, but different users or different script interpreters may be installed in different directories. How does the system know where to find your interpreter? /usr/bin/env tells the system to look in the PATH directory (that is, /usr/bin/env). So configure #! The /usr/bin/env node allows the system to dynamically find nodes to execute your scripts.
How do I run the node command
Just enter the name of the command we define directly on the command line. Here we define test-abc, so just type test-abc
Then we test it out. We type kid into the terminal and it says:
zsh: command not found: kid
Copy the code
Why is that? Recall that when we use a CLI tool, we need to install it first. For example, vue-cli, we need to install it globally:
npm i vue-cli -g
Copy the code
Our kid-CLI has not been released to NPM and has not been installed, so the terminal does not recognize this command yet. If you want to test an NPM package locally, you can use the command: NPM link to install the package locally.
npm link
Copy the code
And then execute
test-abc
Copy the code
You’ll see the output ‘test-abc’