1. Create an NPM account

  • Website: www.npmjs.com/
  • Enter www.npmjs.com/signup to create an account

⚠️ If you do not have an NPM account, you need to create an NPM account. If you already have an account, skip this step.

2. Create a directory

// Run the following command on the terminal: npm-demo is the package directory. You can name it as you like
mkdir npm-demo
// Go to the package directory
cd npm-demo
Copy the code

3. Write modules

Here we use a simple sayHi as an example, with the following code

exports.sayHi = function() {
    return 'Hi, Meta.'
}
Copy the code

Save this code as sayhi.js.

4. Initialize the package description file

Run the NPM init command on the terminal to generate package.json file. As follows:NPM uses a question-like interaction to fill in the entries one by one, and finally generates a preview package description file. If ok, type yes, and you’ll get a package.json file in the file directory.

5. Register a package warehouse account

To maintain packages, NPM must use a repository account to allow packages to be published to the repository.

The command to register an account is => NPM adduser

This is also a question-like interaction, in order.

If the NPM account is not created in Step 1, the NPM account does not exist when you run the NPM adduser command, or you enter an incorrect NPM account information, the following error message will be displayed. Therefore, ensure that the entered user name, password, and email address are the same as the NPM account in Step 1:

6. Upload the package

The command for uploading the package is NPM publish package folder name.

In the directory of the package.json file you just created, execute => NPM publish. Start uploading packages.

In this process, NPM packages the directory as an archive file and uploads it to the official source repository.If the NPM package name and version number already exist, the following error will occur. In this case, you need to rename the name in your package.json file. Even if you don’t find your default package name on the NPM website, it may have been published and needs to be reconsidered. Because I encountered this problem during the upload process, I changed the name in package.json to meta-npm-demo and made sure the package name was unique.

7. Update package

  1. Json, or use the following command to automatically update the version number, and then run NPM publish. Command will do.
  • Upgrade patch version (Modifying bugs) : NPM Version patch
  • Upgrade minor version (new) : NPM Version Minor
  • Upgrade major version: NPM Version Major
  1. Then reinstall the package in the project you are using

8. Test the installation package

  1. In order to experience and test the package we uploaded, we changed the directory
// Run the following command on the terminal: npm-demo-test Specifies the test directory. You can name it as you like
mkdir npm-demo-test
// Enter the test directory
cd npm-demo-test
Copy the code
  1. Let’s write a simple js code and save it to index.js
var demo = require('meta-npm-demo');
var res = demo.sayHi()
console.log(res);
Copy the code
  1. Run NPM install NPM -demo -d

4. Run node index.js

The potential risk

Using NPM in an enterprise’s internal applications is different from using it in the open source community. The limitation for an enterprise is the need to enjoy the low coupling and project organization benefits of module development while also considering module confidentiality. Therefore, sharing and publishing through NPM is potentially risky.

In order to simultaneously enjoy the multitude of packages available on NPM while keeping their packages confidential and restricted, the existing solution is for enterprises to build their own NPM repositories. A local NPM repository is set up in much the same way as a mirror repository. Unlike a mirror repository, an enterprise local NPM can choose not to synchronize the packages in the official source repository, that is, an enterprise can use both the official repository and the local repository.

Internally, private reusable modules can be packaged into a local NPM repository to keep updates centralized and prevent small projects from maintaining identical modules, eliminating code sharing by copy and paste.

Build local NPM warehouse, I will write another article when I have time.

Focus of the module

Browser-side JavaScript loads code over the network, while server-side JavaScript loads code from disk. Node modules are almost always imported synchronously, but if front-end modules are also imported synchronously, the UI initialization process can take a lot of time waiting for scripts to load.

For network reasons, the CommonJs specification for back-end JavaScript is not entirely appropriate for front-end scenarios. Eventually, the AMD specification and CMD specification emerged.

Therefore, in order to make the same module can run in the front and back end, in the process of writing, we need to consider the compatibility of front-end module specifications. To maintain consistency on the front and back ends, the tired developer needs to wrap the library code in a closure.

So we can write this for Node, AMD, CMD, and common browser environments:

; (function(global) {
	"use strict";
    var definition = function(opts) {
        console.log(opts);
    };
	// Check whether the context is AMD or CMD
    var hasDefine = typeof define === 'function';
    // Check whether the context is Node
    var hasExports = typeof module! = ='undefined' && module.exports;
    if (hasDefine) {
    	AMD environment or CMD environment
        define(function() {
        	return definition;
        });
    } else if (hasExports) {
    	// Is defined as a normal Node module
        module.exports = definition();
    } else {
    	// Hang the module's execution result in the window variable. In the browser, this points to the window object
        global.definition = definition();
    }
})(this);
Copy the code