Quantcast
Channel: Active questions tagged require - Stack Overflow
Viewing all articles
Browse latest Browse all 141

How to create a NPM package installed globally to run code from terminal with node js?

$
0
0

I created a javascript with node to rewrite javascricp file to make all require to import and my code do it perfectly. My NPM package is named require2import.it's here on Github.

If run my script from terminal like: npm run require2json src/index.js it will change all require to import. For scanning and modify an entire folder:npm run require2json src

for testing, my file and package.json is in the testing project.

I then tryed to send it to NPMjs but after installing the package require2import globally it do not work. I also tryed locally in the project but it do not find the require2import.js file in the node_modules folder.When installed globally it create a bash .cmd and .ps1 in the right folder in AppData\Roaming\npm on my Windows computer.

I want my script to run on require2json src/index.js from the terminal. (I'm testing it with VS code).But i have error like :

require2import : therme «require2import» is not reconized like a command .......

And if i try locally (installing it directly in the project as devDependencies or dependencies) it try to find the require2import.js in the project but not in the node_modules.

Suggestion from openai and the like said to add this on top of my .js file:

#!/usr/bin/env node

And putting this in bin in the package.json :

"bin": {"requiretoimport": "./require2import.js"  },

When i was working on it on my computer before publishing it on NPM i had this too in scripts in my package.json.

"scripts": {"require2import": "node ./require2import.js","r2i": "node ./require2import.js"  },

But i think i have to remove this part to work from a package installed from NPMjs or for global installation.

To find more about the code you can go to the 2 mains files on Github.


Viewing all articles
Browse latest Browse all 141

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>