0

I have a .TS file where I store all of my utility functions that I use in alot of my projects (kind of like my own private lodash) However every time I want to use this I have to manually copy and paste this file into the project that I am working on. I also know how to use node to make scripts and write to the file system using FS, my question is how do I make this node script available globally without having to specify ".js" so I can use it in the terminal like this: "node loadLibrary"?

TL;DR
How do I make .js files available to node globally so I do not have to specify the path and just call the script by name EX: "node loadLibrary"

4
  • You can manually copy a module into the same location that npm -g install will install a module globally and then any project can use that module with just import "someLibrary" or require("someLibrary") Commented Jun 26, 2020 at 21:48
  • When I deploy my code will the module be available outside of my pc if I include it in my package.JSON? Commented Jun 26, 2020 at 22:02
  • No. If you want to make it part of your project so it goes with your project when you deploy it somewhere else, then you need to install it locally so it's part of your project directly. Or, you can put the code in a networked repository (public or private) and refer to it in your package.json as a dependency so that when your project is installed elsewhere, it will fetch it as a dependency. You seem to be asking for several conflicting things which is why you can't really get everything in one solution. Commented Jun 26, 2020 at 23:04
  • My first comment was addressed as your specific question as originally written. You didn't say anything in your original question about deployment elsewhere which changes the requirements significantly. So, for people to be able to help, you need to lay out the entire set of requirements/wishes at once. Commented Jun 26, 2020 at 23:05

2 Answers 2

2

Maybe you could create Git Repository with the functions.
In the projects you want to import the functions you can do:

npm install --save https://github.com/foo/bar.git /* your code */ import { helperFunc } from 'bar'; 
Sign up to request clarification or add additional context in comments.

Comments

1

Have you considered just making it an npm package? Might not be the exact solution you were thinking of, but it would certainly be easy.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.