Home
Nacho's Blog

Publishing an NPM Module

NPM modules are one amazing creature comfort for the modern JS developer. In my case, I wanted to publish a tool to fix an (issue with Next.js export format)[https://blog.ignaciodegregori.com/posts/how_to_fix_next_js_exporting_pages_as_files].

Developing the code

This is the part we were all waiting for, where we show off our coding skills. But there are a few peculiarities to take into account. In order to build an executable CLI. Our package.json will have to include a bin section where we name the command to be called to execute our code.

  "bin": {
    "yourCommand": "./bin/cli.js"
  }

Note that I named the called file cli.js, that's by convention and the file only really handles what function to called and passing the parameters into it.

Publishing

This part is extremely easy, once you have your code ready you just need to run:

npm login
npm publish

And that's it!

If it fails, it is probably because there is already a package with the same name as the one you are trying to make.

Once the publishing finishes, all the data is obtained from package.json so no more steps are necessary.

This includes Author, License, Repo, Website and more.

Its also almost obligatory to add a good README.md to act as a frontpage in npmjs.com and in our repo's page. Remember to add god usage instructions besides a basic description.

Conclusion

Overall, the process is painless if you have any experience in starting projects and handling packages. You can see my final repo here.

Published: Tuesday, Sep 5, 2023
Privacy Policy© 2023 Ignacio Degregori. All rights Reserved.