4

I have a static .npmignore file with

foo
bar
baz

in it. When I publish to NPM, the contents in those 3 dirs will be ignored.

My question is, is there a way to dynamically add a folder to ignore when using npm publish at the command line?

Something like:

npm publish --ignore=.r2g

here we can ignore a folder called .r2g

Here are the npm-publish docs

hong4rc
  • 3,999
  • 4
  • 21
  • 40
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 1
    There's nothing builtin to achieve this that I'm aware of. You can utilize npm-scripts `prepublish` (or `prepublishOnly`) hook to append folder to _.npmignore_. E.g. `"prepublish": "echo \".r2g\" >>.npmignore"`. Then use `postpublish` hook to remove the previously appended folder from _.npmignore_, effectively resetting it. E.g. `"postpublish": "cat .npmignore | tail -r | tail -n +2 | tail -r >.npmignore"` - this removes the last line. For cross-platform support consider invoking _nodejs_ scripts containing similar logic from the `prepublish`/ `postpublish` hooks. – RobC Sep 30 '18 at 11:54
  • 1
    Another way is [`npm pack`](https://docs.npmjs.com/cli/pack) to create tarball, (instead of `npm publish`), and use a `postpack` script in _pkg.json_. The node script `postpack` invokes can utilize [node-tar](https://github.com/npm/node-tar) to effectively **1.** Unpack tarball. **2.** Remove unwanted dir(s). **3.** Create new tarball. (Analogous to [this example](https://github.com/matt-block/react-native-in-app-browser/blob/master/scripts/postpack.ts)). This way you can also omit redundant `postpack` script and `node-tar` devDependency from _pkg.json_, leaving no trace in what's published. – RobC Oct 24 '18 at 10:29
  • @RobC that is a good idea although a bit intrusive, tbh i prob wont do it that way but i will think abou it – Alexander Mills Oct 24 '18 at 16:05
  • Yeah, I totally agree about it being intrusive - hence comment only. Unfortunately, all solutions I can think of entail some kind of _"pre/post"_ npm-script hook - (`npm pack` being my most preferred). – RobC Oct 24 '18 at 17:23

1 Answers1

5

First, a static solution:

You can include a .npmignore file per directory that you want to exclude. As the doc states:

Like git, npm looks for .npmignore and .gitignore files in all subdirectories of your package, not only the root directory.

I created an example of this in https://github.com/dral/npmignore-example (and the corresponding package npmignore-example).

file structure index.js .npmignore bar/index.js baz/index.js foo/index.js /.npmignore

the root .npmignore removes only the baz directory. the nested foo/.npmignore removes all content (from this point on).

The installed package includes only

index.js
bar/index.js

In order to do so dynamically you can use a script that adds and then removes a simple .npmignore file in the selected folder(s).

echo '*' > .r2g/.npmignore && npm publish && rm .r2g/.npmignore

Then if you need to automate this, consider using prepublish and postpublish scripts that take into account an env variable so you can use something like NPM_IGNORE=.r2g npm publish.

Hope this helps.

dral
  • 176
  • 3