DEV Community

Michael Willian Santos
Michael Willian Santos

Posted on

My first NPM module

#start

Hi everyone!
GitHub of the Project


For my first post here I have think about sharing my first NPM module.

First of all I'm not an newbie into coding neither on JavaScript language, however I never thought to share some application that I write, that in overall helps me in production time and so on.

But, getting so much from open source and community, I have decided that it's time for me to start sharing some application, libraries and snippets that I usually write and use on my projects.


#vorlefan.path_route

What is it about? The main functions is to given the power of easily manage the access to the folders and files of your project. With the ability to handle files (example: read json file)

Why I have created this? I was tired of typing '../../../../someName/file.json' everytime on my projects. Or being almost all time using 'path.resolve' to not get some errors.


#example

In my current project that I'm working at the back-end with NodeJS and at the front-end with React (NextJS). I have a folder named 'server', inside of this folder I have my structure of folders and two javascript file. 'index.js', that executes the server and 'path_route.js', that handles with the structure of my path routes. Take a look:

The main reason that my 'path_route.js' is at the main folder, is just to have the __dirname directed to the main folder. This make easy my life.

Now the code (I'll not show up the complete code to not make this post big)

With this piece of code, I can access my folders easily, example:

To access my 'assets' folder:

Route.Main.get('assets')

// it returns

{ 
  "name": "assets",
  "path": "__dirname/assets"
}

And my module.exports to make it more general for my overall application is:

With this I'll only need to require this file, to access the overall structure of folders and files of my project.

// Access the folder structure of my folder 'assets'.

const Assets = require('../../../path_route').load('Assets')

#end

Sure things there are a lot of room to improvement and features to add, however I'm pretty enjoyed this module of mine and to be first in both publishing and experience in using 'npm publish', I feel that it's okay xD.

It would be nice to receive some feedback and suggestions :)

Top comments (0)