zanerock
zanerock

Reputation: 3572

How to exclude 'bin' files from NPM package?

EDIT: Not sure what happened in the initial test, but .gitignore and .npmignore are now working to exclude the bin files as desired.

I have a simple NPM package which just installs a binary by downloading it from GitHub. My goal is to leave the binary out of the published package and just let the script install it. My thinking is that the install script may need to install different binaries depending on the host platform, and secondarily, there's no need to bloat the package size with the binaries.

The problem is that I can't seem to get NPM to ignore the 'bin' files. I've tried using .gitignore, .npmignore and the package.json 'files' attribute as well as placing the bin files in a folder other than bin, and it seems that NPM insists on including the 'bin' files. The relevant package.json is basically:

{
  "bin": {
    "my-bin": "./bin/my-bin"
  },
  "files": [ "bin-install.sh" ],
  "scripts": {
    "preinstall": "./bin-install.sh"
  },
}

As a workaround, I can juggle the 'bin' directory existence like:

"scripts": {
  "preinstall": "./bin-install.sh",
  "prepublishOnly": "! [[ -d bin ]] || mv bin tmp",
  "prepack": "npm run prepublishOnly",
  "postpublish": "! [[ -d tmp ]] || mv tmp bin",
  "postpack": "npm run postpublish"
}

This works, but looks ugly and makes me think I'm missing something that's leading me to go against the grain.

Is there a cleaner approach to achieve what I'm trying to do?

Upvotes: 1

Views: 1272

Answers (1)

YakovL
YakovL

Reputation: 8347

.npmignore should work, here are the docs. You haven't shown how exactly you've attempted to set up ignore, but it sounds like you has been trying to ignore all files inside bin/ so this is what you needed:

# inside .npmignore
bin/

Upvotes: 2

Related Questions