DEV Community

Devin W. Leaman for 4lch4 Industires, LLC.

Posted on • Edited on

New Node Modules w/ PowerShell

I've never been much of an OS/Platform purist. For as long as I can remember I have switched between Unix/Windows based systems on a regular basis for various reasons. With that said, I develop primarily on a Windows machine, and most of my projects end up running on a Linux server (Node modules), or on a Linux kernel (Android) of some sort, so being familiar with the *nix school of thought is quite helpful in the long run.

Coding_Image.png

Without getting too thick in my personal examples of what it's like living between platforms, I'd instead like to share some of the stuff I've written or tools I use to make my life a bit easier.

This post explicitly covers a script I wrote and added to my Current User, Current Host - console PowerShell Profile to make initializing new Node.js modules much easier and specific to my preferences.

If you'd like, you can skip straight to the code on GitHub if that's more your style, or read on for my explanation of my problem and my solution.

What was the problem?

Well, it wasn't a problem so much as a nuisance. When creating a new Node module, my standard workflow was something like so:

  1. Open Visual Studio Code
  2. Go to File -> Open Folder
  3. Navigate to my Node.js modules folder -> Create a folder for the module
  4. Open the newly created folder and launch the integrated terminal
  5. Use npm init to setup the package.json with my default values
  6. Use touch index.js; code index.js to create the new file and start work

To say this is a bit tedious and annoying would be an understatement.

What's my solution?

I created a PowerShell Script called New-NodeModule.ps1, with a single function New-NodeModule, that I dot-source in my Profile.ps1. The function does the most common things I do when creating a new module, such as create a package.json with the default values for the license, author info, and so forth.

The only required information to create a new module is the name of the module, which is accepted as the first parameter or using a named parameter like New-NodeModule -ModuleName outside-cli. If desired, you can set the location for the module to be stored using the -ModulePath parameter. Moreover, regarding the accepted parameters, I prefer to open the newly created module in Visual Studio Code right after creating it so there's a -OpenInVSCode parameter that defaults to $true but can be set to $false to prevent opening the newly created module.

What're the default values?

When creating a new Node module, I tend to use a lot of the same settings. For example, I always use Jest for testing purposes and all of my projects are formatted/linted with StandardJS. More specifically:

  1. As I'm creating the module, I can rest assured that I'm the author, so I'll always have the author block filled with information about myself
  2. I always have the start script as a simple node .\index.js
  3. I mentioned I test with Jest, so my test script will always start with jest
  4. I prefer my projects to be licensed under the MIT License as it most aligns with my views on OSS

For more specifics, refer to the source code itself as it's all right there. πŸ€“

Conclusion

To conclude, the last significant change I made to make it easier to use this new file was to add aliases to the New-NodeModule function (nodemod, new-nodemod, newmod, newmodule, newnpm). Now, whenever I want to create a new module, I simply type newmod in my terminal and it does what I need 😊

If you have any questions, comments, or concerns, please feel free to contact me directly or comment on the post!

Top comments (2)

Collapse
 
ephermalite profile image
ep

tx for sharing, quite helpful

Collapse
 
4lch4 profile image
Devin W. Leaman

No problem! I intend to update it if I have any other ideas or npm has some new features come out but if you have any suggestions let me know and I'd be glad to see how to incorporate them.