Home > database >  NodeJS Bin Access is denied
NodeJS Bin Access is denied

Time:07-28

Details

I am creating a simple CLI to download music from youtube and apply spotify metadata to it. But while trying to use the binary I am having issues which I don't really understand. I'm kinda new to NodeJS, I tried to find a solution but I couldn't. Any help is appreciated

directory

 Directory of C:\Users\soham\Documents\spot-dl

27-07-2022  22:28    <DIR>          .
26-07-2022  20:50    <DIR>          ..
27-07-2022  17:43    <DIR>          node_modules
27-07-2022  17:50           165,129 package-lock.json
27-07-2022  22:28               536 package.json
27-07-2022  22:12             5,522 spot-dl.js
               3 File(s)        171,187 bytes

package.json

{
  "name": "spot-dl",
  "version": "1.0.0",
  "description": "Download music from Spotify.",
  "main": "spot-dl.js",
  "scripts": {
    "test": "npm start",
    "start": "node spot-dl.js"
  },
  "keywords": [
    "spotify",
    "music"
  ],
  "author": "Soham Borate",
  "license": "MIT",
  "dependencies": {
    "fs": "^0.0.1-security",
    "https": "^1.0.0",
    "node-id3": "^0.2.3",
    "spotify-web-api-node": "^5.0.2",
    "stream": "^0.0.2",
    "youtube-sr": "^4.2.0",
    "yt-converter": "^1.3.1"
  },
  "bin": "spot-dl.js"
}

terminal

C:\Users\soham\Documents\spot-dl>npm install -g .

added 1 package, and audited 3 packages in 693ms

found 0 vulnerabilities

C:\Users\soham\Documents\spot-dl>spot-dl
Access is denied.

Node.js version

v16.13.1

Example code

Not applicable.

Operating system

Windows 11

Scope

bin

Module and version

Not applicable.

CodePudding user response:

If you want your app to be executable, you have to do multiple things, the npm -g . command does not move your project to your /bin directory, which means that you can not execute it from the terminal this way.

first, add the shebang line at the very top (the first line) in your entry file as follows:

#!/usr/bin/env node
// the rest of your code

now, this file works on most Linux/Unix operating systems, if you're on a Linux/Unix based operating system, do the following:

  • give the permissions to become executable:
chmod  x ./<YOUR_ENTRY_FILE>

If you're on windows, add the following in your package.json file:

"bin": "./YOUR_ENTRY_FILE" // for your case it's "bin": "./spot-dl.js"

Now, the package can be run on windows and on Linux, the "bin" part of the package.json, Npm will check to see if it is installed on windows and it will install a .cmd wrapper alongside your script so users can execute it from the command-line.

Edit: for your case, your entry file is the spot-dl.js file.*

  • Related