Here is a brief introduction to the process of NPM creating function modules, uploading them to network libraries and referencing them.
Please skip to the bottom of this post and click like. Thank you very much.
Question 1: What does Node.js do?
Node.js is a server JS runtime environment. There is a more praise high answer on the Internet, just a sentence: free from the browser js runtime environment. The browser has its own JS runtime environment (which JS engine is not discussed here). If front-end developers are comfortable with using JS to process logic, why not put js runtime environment on the server for front-end developers to develop server logic? This is not can better play the role of the programmer.
By not only putting the JS engine on the server, but also extending some new syntax, node.js is created.
Q2: What does NPM do?
Every development language should have a third-party library management tool, and NPM is understood to be node’s third-party library management tool.
Those who seek scientific rigour may be dismissive of such an answer. But for beginners, how to better understand what “it” is in simple language, I think is particularly important.
Let’s cut to the chase and get to the logic
Step 1. Create a new project
Create a folder named “wsl-date-manage”, open it on terminal, and run NPM init:
You will be prompted to perform some operations on the terminal. You can enter basic information according to related operations. When done, a package.json file is generated. As shown in figure:
Now create a new index.js file to implement a simple date printing method.
Run index.js on the terminal. Run the node index.js command
Here you can see “2021-06-22” printed out.
The next step is to pack this little function into a package. Before packaging, the last sentence will be printed as an export instruction.
To perform the packaging operation, the terminal enters the command: NPM pack
Create an npmDemo project and install the local package:
CD into npmDemo project executes instructions: NPM install/Users/ganggu/Desktop/WSL – date – the manage/WSL – date – manage – 1.0.0. TGZ
Now look at the node_modules folder and the package is imported:
Use this package
The results
Upload the NPM network library,www.npmjs.com/
No account needs to register an account in the above website; Log in to the terminal as NPM adduser
release
Perform NPM Publish; Here we need to pay attention to the problem of switching NPM source, generally will switch to Taobao source, so the release will prompt no rights, need to switch to NPM source first, after the release is completed to switch to Taobao source;
npm config set registry registry.npm.taobao.org
npm config set registry registry.npmjs.org
Finally, a successful release is displayed:
Refer to this network library
Uninstall the local reference library before testing. To be safe, you can change the path of the previous library to prevent NPM from loading the local library again:
At this point, simple NPM library creation, local references, and network library references are complete.
End: to here the basic train of thought is finished, do not have too complex logic, expression ability is limited. Hope to everyone can be helpful, but also accept the criticism of the gods, common progress.