Detailed explanation of examples of private npm packages

零下一度
Release: 2017-07-17 16:10:05
Original
2033 people have browsed it

Nowadays, every language system has a package management tool, PHP's Composer, Ruby's gem, Python's pip, Java's Maven... and of course Node.js's npm. Some people may wonder why we need to introduce another new thing to make our already hard enough programming work worse? In fact, this is not the case. For example, when we are doing Java development, some projects rely on hundreds of jars. Developers will always encounter problems with various package versions and package cannot be found before building. However, package management tools allow us to Rescue from this dependence nightmare. Any dependency during our development process can be automatically downloaded from the remote package repository through this tool, and it is guaranteed to be the version we need, so that we can focus on development and production.

This article refers to some scattered information on the Internet and compiles a relatively systematic introduction to the release of npm packages. Here is the useful information:

1. Download node

Address:

After the installation is completed, you can open the cmd command line to test whether the installation is successful

If the version appears, the installation is successful.

2,

OK The installation work is ready, let’s start writing a simple module!

Create a working directory anywhere you like! I created it in the d:/nomDemo directory.

Create a new js file, name it a.js, enter the following code

function hello(name){
console.log("hello"+ name);
}
exports.hello=hello;

Super simple! There is only one hello function!

Note:

exports.hello=hello;

This sentence is the key! Use exports to expose your hello function! If you don’t understand, you can Baidu CommonJs specification!

Next we create a b.js file in the directory! The code is as follows

var h=require('./a');
h.hello('jihuaqiang');

You can see that the b.js file only has two lines! The first line first uses require('./a') to import the a module just now, and then we call the hello method in the module! Very simple.

ok, everything is over! Although the two files add up to only 5 lines of code, it is enough for us to demonstrate what we want! (Sometimes simplicity makes it easier to understand).

Let us use node to execute it. It is also very simple, and the execution code only has two sentences! The output result is hellojihuaqiang.

3. Publish this module

First enter the underlying directory and execute the npminit command

This command will create a package.json file in the current module. The purpose is to set your own module name, version, author and other information

ok, you must have your own npm account to upload. You can go to the npm official website to register, easy

and then continue to return to the module directory and type Run the following command

After logging in, execute the npm publish command to complete the upload

4. Verification

Enter a directory at will. Here I return to the upper directory and install this module.

Output after quoting "hellojihuaqiang".

Finish.

The above is the detailed content of Detailed explanation of examples of private npm packages. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!