简体   繁体   中英

How do I use a .js file as a node command globally?

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"

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';

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM