This is the second day of my participation in Gwen Challenge
I’ve been working on front-end projects for the last few months, and then I can just type NPM install XXX to install the dependencies locally or globally, and even execute NPM CI in a project that contains package.json files to automatically introduce the dependencies. Well, I was just wondering, can I release the NPM package? Then a pointless attempt was made to keep a record.
The early stage of the research
So first of all, how do you distribute an NPM package?
The steps are as follows:
- Step one: Sign up for an NPM account. It’s free
- Step 2: Create a new project and pass it
npm init
Perform initial configuration for the project - Step 3, enter the project directory and execute
npm publish
It can be published to the NPM public repository for download.www.npmjs.com/).
Note: you may encounter various minor problems during operation, but you can solve them by looking at the error log. There are only three simple steps. The following details.
Create a meaningless project
Since there is no meaning, there must be fun, so I referred to a project on Github: five, I created a project two.
The next step is to improve the project. On top of that, I searched NPM and found that the name “two” was already taken, so I had to think of another one or the launch would fail, and added the prefix “monkey-two”.
Initialize the project
- Create a project directory
./monkey-two
- Go to the project directory and execute
npm init
Json file. You can either type yes or press Enter.
Perfect project
- Create a file like index.js and import it in this file
two.js
Then all the major functions and implementations are done in this file - Writing tests, how can a great project not have tests? add
test.js
The file that this project tests is usedmocha
To carry out the - Prepare readme. md, this file is mainly to explain the use of the project and the applicable scenarios, and especially very important, must attract attention, this point I have not done, will be improved in the future, such as adding a variety of labels, add colors and pictures, what
What can this project do
Basic 2 (Basic number 2)
two(); // 2 two.valueOf(); / / 2Copy the code
In Addition
two() + two(); // 2 + 2 = 4
two.add(1); // 1 + 2 = 3
two.add(2); // 2 + 2 = 4
two.add(3); // 3 + 2 = 5
two.add(10, 5); // 10 + 5 = 15
Copy the code
Traction (Subtraction)
two() - two(); // 2 - 2 = 0 two.subtract(1); // 1 - 2 = -1 two.subtract(2); // 2 - 2 = 0 two.subtract(3); // 3 - 2 = 1 two.subtract(10, 5); // 10-5 = 5Copy the code
Multi (Multiplication)
two() * two(); // 2 * 2 = 4
two.times(1); // 1 * 2 = 2
two.times(2); // 2 * 2 = 4
two.times(3); // 3 * 2 = 6
two.times(10, 5); // 10 * 5 = 50
Copy the code
A car using a multi.
two() / two(); // 2 / 2 = 1 two.divide(1); // 1/2 = 0.5 two. Divide (2); // 2 / 2 = 1 two.divide(3); // 3/2 = 1. Divide (10, 5); // 10/5 = 2Copy the code
Power (Power operation)
two.power(); // 2 two.power(3); // 8 two.power(10); / / 1024Copy the code
Take the Square.
two.square(); // 1 two.square(4); // 2 two.square(1024); / / 32Copy the code
Different radices
two.base(2); // 10 two.base(8); // 2 two.base(10); // 2 two.base(16); // 2 # the base is 2 (binary).baseof (); // 01 # default 1 two.baseOf(10); / / 1010Copy the code
Different Sorts of 2 (Fancy output 2)
two.upHigh(); / / squared two downLow (); / / ₂ two Roman (); / / Ⅱ two Chinese (); / / 2 two Chinese (" pinyin "); / / er two Chinese (" financial "); / / two two Japanese (); / / 2 two English (); // two two.upperCase(); // TWO two.repeat(); // 2 two.repeat(5); // 22222 two.repeat(10); / / 2222222222Copy the code
Date (Date dependent)
two.dayOfWeek(); / / Tuesday two dayOfWeek (" EN "); // Monday two.monthOfYear(); / / February two monthOfYear (" EN "); // FebruaryCopy the code
Unicode Emoji
two.peace(); / / ✌ ️ two victory (); / / ✌ ️ two eyes (); / / 👀 two oclock (); / / 🕑 two oclockStatus (); // 🛌 # default 2:00am, you should be sleeping in the bed. two.oclockStatus("PM"); // 👨💻 # 2pm, shoulding be coding. !). two.oclockStatus("pm"); / / 👨 💻Copy the code
To Assert
two.isTwo(); // true
two.isTwo(2); // true
two.isTwo(3); // false
Copy the code
Compare:
two.bigger(1, 2); // 2 two.smaller(1, 2); / / 1Copy the code
Publish the project
Once the basic functionality of the project is complete and ready for release, perform:
npm publish
Copy the code
At this point, an error may be reported because the NPM account has not been logged in, so executenpm login
Enter the user name and password:Finally, just publish, and here’s a screenshot of the success:
validation
The verification method is also simple:
Method 1: use the NPM install command to check whether the installation is possible. Method 2: Go to the NPM website to search, remember to use the full name of the search, and wait a period of time to search, to find.
Next, useRunKitJust verify:
The project address
Github.com/monkey-play…