Below, a small guide to publishing your own packages on npm.
You have written some code that you think is really useful! Now you want to publish it on npm so others can use your great package!
There are many configurations for npm but this article will only cover the essentials for your packages to be published into npm.
npm is included with Node.js. To check if npm is installed on your system, run this command on your terminal:
npm -v
Create folder
1 2 3 4 5 | <span class="token shell-comment comment"># This will create, and navigate</span> <span class="token shell-comment comment"># into the `test-npm-bundle` directory</span> mkdir test <span class="token operator">-</span> npm <span class="token operator">-</span> bundle cd test <span class="token operator">-</span> npm <span class="token operator">-</span> bundle |
Initialize npm
We will initialize npm with the command
1 2 | npm init |
Running npm init will ask you a few setup questions (for example, your package name, your package description, etc.).
You can press Enter Enter Enter for each question and this default summary for package.json
will be created in your directory:
1 2 3 4 5 6 7 8 9 10 11 12 | <span class="token punctuation">{</span> <span class="token property">"name"</span> <span class="token operator">:</span> <span class="token string">"test-npm-bundle"</span> <span class="token punctuation">,</span> <span class="token property">"version"</span> <span class="token operator">:</span> <span class="token string">"1.0.0"</span> <span class="token punctuation">,</span> <span class="token property">"description"</span> <span class="token operator">:</span> <span class="token string">""</span> <span class="token punctuation">,</span> <span class="token property">"main"</span> <span class="token operator">:</span> <span class="token string">"index.js"</span> <span class="token punctuation">,</span> <span class="token property">"scripts"</span> <span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"test"</span> <span class="token operator">:</span> <span class="token string">"echo "Error: no test specified" && exit 1"</span> <span class="token punctuation">}</span> <span class="token punctuation">,</span> <span class="token property">"author"</span> <span class="token operator">:</span> <span class="token string">""</span> <span class="token punctuation">,</span> <span class="token property">"license"</span> <span class="token operator">:</span> <span class="token string">"ISC"</span> <span class="token punctuation">}</span> |
What is Package.json?
You see package.json as a recipe for your favorite food
Package.json contains all the metadata that describes the project (for example, potato pie) and all the dependencies needed to run it (ingredients: potato, flour, sugar, etc.).
Using all of this information, npm brings everything together so that your package can be easily downloaded and run by others.
Feel free to edit package.json to include a description and author information.
1 2 3 4 5 6 7 8 9 | { "name": "test-npm-bundle", "version": "1.0.0", "description": "lần đầu làm chuyện ấy", "main": "index.js", "author": "KhoiPv < <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="017169606f2f77606f2f6a696e684172746f2c607275647368726a2f626e6c">[email protected]</a> >", "license": "ISC" } |
The only required fields in package.json are "name", version, "version" and "main". The "scrpit" has been deleted because we don't have any tests written.
1 2 | touch index.js |
Create the index.js
file and add the following
1 2 3 4 5 | module <span class="token punctuation">.</span> <span class="token function-variable function">exports</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">'Hôm nay thật tuyệt vời'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">;</span> |
Remember to export your code the same way you do for local files in your project.
Create a README
Generally, README is often used for the purpose of letting others know how to use your package.
Please create the README file in your root directory:
1 2 3 | touch README echo "## Làm theo như trên là được!" > README |
And finally Publish
Currently, the package looks like this:
1 2 3 4 5 | test-npm-bundle |_ index.js |_ README |_ package.json |
Basically, this is the basic structure of the npm package.
Now we can publish it!
1 2 | npm publish |
You will need an account on the npm registration website and if you have not logged in to it from CLI, you will be asked to login. You must also use the package name already in use
The end
Finally, a summary of the 3 steps to publish a package is as follows
- Initialization: npm init
- Add the code: index.js and create README
- Publish: npm publish
Now if you want to use that package, run:
1 2 | npm install test-npm-bundle |
It will run and install the necessary dependencies.