In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to write the node file into a npm package and release it, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Copy the node file into a npm package and release it
Npm plug-in release
Publishing npm is actually a very simple thing, just because you will forget it if you don't release it for a long time, and you have to check it on the Internet, so write an article to record it.
Create a new file directory
Create a new directory with any name
Run the command to generate package.json
Npm init-yes
Installation dependency
If you need other dependencies in your project, you can install them through npm install xxx as in normal development, but one thing to note here is the difference between-S,-- save, and-- save-dev, because there is usually no essential difference between the three when developing a project, but there is still a difference in developing npm packages.
The plug-ins downloaded by-S and-- save will be written to dependencies, and when we install the custom plug-ins, they will be downloaded together
-- plug-ins downloaded by save-dev will be written to devDendencies, which is only used during development and will not be installed with custom plug-ins
Perfect package.json
There are several important information that must be filled in.
Name other people need to install this plug-in through npm install xxx. This xxx corresponds to name's value.
The version of the version plug-in needs to be renewed every time it is rereleased, otherwise the launch will fail.
Main entry file
Others can be read and filled in.
{"name": "node-fs-copy", / / the name of the package published. The default is the name of the parent folder. Must not repeat the package name in the current npm. Package names cannot have uppercase letters / spaces / slides! "version": "1.0.0", / / the version of your package. The default is 1.0.0. There are a series of rules for the version number of the npm package, the version number of the module is in X.Y.Z format, specifically reflected in: 1, repair bug, minor changes, increase z. 2, the addition of new features, can be backward compatibility, increase y 3, there are great changes, can not be compatible downward, add x "description": "," main ":" index.js ", / / entry file, the default is Index.js, can be modified into their own file, this is very important, when you are in the actual project use, let a = require (" package name "), it will go to find the corresponding file path. "scripts": {/ / shortcut command. Entering the command npm run key in the same directory as package.json will execute the corresponding command "bulid": "npx webpack-- config myConfig.js" / / for example, typing npm run bulid will execute the command npx webpack-- config myConfig.js. }, "keywords": [/ / keywords for npm search "node", "fs", "copy"], "publishConfig": {"registry": "" / / published npm address}, "repository": {"type": "git", "url": "git address of git+ https://github.com/xxxx" / / code}," author ":" zxw " "license": "ISC", / / enter directly Open source file protocol bar, can also be MIT, depending on the need. "dependencies": {/ / package "jquery": "^ 3.4.1", "sea": "^ 1.0.2"}, "devDependencies": {/ / package "webpack": "^ 4.41.6"}}
After determining that the entry file is index.js, and write the code, note that both import and export need to be done through node.
Index.js
Const {exists, copyDir} = require ('. / lib/copy') const fsCopy = (sourcePath,deptPath) = > {exists (sourcePath,deptPath, copyDir)} module.exports = {fsCopy}
/ lib/copy.js
Const fs = require ('fs') / * copy the files under one folder to another folder * @ param src source folder, that is, the files to be written out * @ param dst destination folder Files to write * / const copyDir = function (src, dst) {/ / read all files / directories in the directory fs.readdir (src, function (err, paths) {if (err) {throw err} paths.forEach (function (path) {const _ src = src +'/'+ path const _ dst = dst +'/'+ path let readable Let writable fs.stat (_ src, function (err) St) {if (err) {throw err} / / determine whether it is a file if (st.isFile ()) {/ / create a read stream readable = fs.createReadStream (_ src) / / create a write stream writable = fs.createWriteStream (_ dst) / / through a pipeline To transport the stream readable.pipe (writable)} / / if it is a directory, recursively call its own else if (st.isDirectory ()) {exists (_ src) _ dst, copyDir)}})})} / * * determine whether the current target file exists * if it does not exist, create it first * * / const exists = function (src, dst, callback) {/ / if the path exists Otherwise, true is returned, otherwise false is returned. If (fs.existsSync (dst)) {callback (src, dst)} else {fs.mkdir (dst, function () {callback (src, dst)})} module.exports = {exists, copyDir}
test
I only conducted a relatively simple test in this area, and I will fill in a chapter on plug-in testing later.
Publish
Sign up for a npm account. Usually, npm can log in directly through associated gitlab.
Make sure that your current image points to the npm image instead of Taobao image. If you are not sure, you can execute it directly.
Npm config set registry https://registry.npmjs.org/
Run the add user command, enter the user, password, mailbox in turn.
Npm addUser
Ensure that the current npm has executed the publish command when the browser is logged in
Npm publish
As shown in the picture, the release is successful.
Publish an error report
If the release encounters a 403 error, it is very likely that your package name, that is, the name field in package.json, duplicates the existing plug-in name of npm. You need to modify it before republishing it.
An error is still reported after the name has been modified. You cannot publish over the previously published versions: xxx indicates that this version already exists in npm, and the version number needs to be modified.
Iteration
If there is any change in the subsequent content, you need to manually change the version number of package.json/version every time you reissue it, and then execute the issued command.
Use the example
Installation
Npm install node-fs-copy
In node code, the local copy test
Const {fsCopy} = require ('node-fs-copy') / / copy the contents from the test/test directory of the local D disk to the test/test1 directory fsCopy (' dghue hand Testhand testbank, 'dpurge hand Testhand test1')
Server code copy
There is no way to copy the server code directly. If you need to copy the server code, you need to meet a condition.
Node server code on the same server as the files that need to be copied
For example, the file address on the author's server is / data/code-generator,node service is also deployed in another directory of the same server
/ / run on the server, which means copying the contents of the server's / data/code-generator file to the fsCopy ('/ data/code-generator','. / temporary/test') in the. / temporary/test of the current project.
After the copy is completed, you can use the packaging plug-in to compress the content into a zip package, output it to the front end, then delete the temporary file. / temporary/test, and then delete the zip package
Attach the common command npm init-yes (initialize configuration) npm I (will install the corresponding package according to the key dependencies,devDependencies in package.json) npm I package (install a latest package by default, this package is in the node_modules folder, and will be updated in your package.json file) npm I package @ 3.0.0 (install a specified version of the package Will update in your package.json file) npm I package-save-dev (install a package required by the development environment, will update in your package.json file) npm uninstall package (uninstall a package, will update in your package.json file) npm update package (update this package version to the latest version) Will be updated in your package.json file) npm run script key (the relative value will be automatically executed according to the script key in "scripts" in package.json) npm publish (publish a package according to package.json 's name) npm unpublish package name-force (uninstall the package uploaded by yourself on the npm website) Thank you for reading this article carefully I hope the article "how to write node files into npm packages and publish them" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.