Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop a NPM package in javascript

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to develop a NPM package in javascript" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "how to develop a NPM package in javascript" article can help you solve the problem.

Preface

Nowadays, the front-end development has changed from the traditional manual introduction of cdn resources to an engineering development system, the introduction of external resources are through npm installation dependencies, import dependencies for use. How to develop a npm dependency package is not difficult. This article will show how to develop a simple npm dependency package, develop and upload the npm platform, you can install and use in the project.

Register for npm

If you want to develop the npm dependency package to be downloaded and used by others, you need to upload the final development project to the npm platform for management. First go to the npm platform to register an account, the registration process is very simple.

Create a project

Create a new folder javascript-test (the name can be customized)

In the javascript-test terminal console, execute npm init, fill in as prompted, and finally generate a package.json project description file

{

"name": "javascript-test"

"version": "1.0.0"

"description": "npm-test"

"main": "index.js"

"scripts": {

"test": "echo\" Error: no test specified\ "& exit 1"

}

"keywords": [

"npm-test"

]

"author": "Hunter"

"license": "ISC"

}

Create an index.js file, the project entry file, in the javascript-test root directory, corresponding to the main configuration in the package.json file

Function init () {

Console.log ('Hello Worldwide')

}

Export {init}

The javascript-test terminal console executes npm login, and according to the prompts, enter the account password to log in to npm

After logging in successfully, execute npm publish to publish (Note: the name configuration in package.json file is the name of npm dependent package, which cannot be duplicated with the name of dependent package on npm platform, otherwise publishing fails)

After the release is successful, you can see your dependency package on the npm platform.

Installation and use

Execute npm install javascript-test in another project-save installs the dependency package developed by yourself. After the installation is successful, you can see it in node_modules.

Import init and execute this method

Import {init} from "javascript-test"

Init ()

Console prints successfully

This is the end of the introduction to "how to develop a NPM package in javascript". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report