npm Community Forum (Archive)

The npm community forum has been discontinued.

To discuss usage of npm, visit the GitHub Support Community.

.gitignore is not working properly

What I Wanted to Do

Publishing a package, assets/node_modules/a.js is ignored by .gitignore

What Happened Instead

There is still a assets/node_modules/a.js in the package.

Reproduction Steps

λ   tree -a
├── .gitignore
├── assets
│   └── node_modules
│       └── a.js
├── node_modules
│   └── b.js
└── package.json

3 directories, 4 files
λ   cat .gitignore 
λ   npm pack
npm notice 
npm notice 📦  npm-ignore@1.0.0
npm notice === Tarball Contents === 
npm notice 253B package.json            
npm notice 0    assets/node_modules/a.js
npm notice === Tarball Details === 
npm notice name:          npm-ignore                              
npm notice version:       1.0.0                                   
npm notice filename:      npm-ignore-1.0.0.tgz                    
npm notice package size:  297 B                                   
npm notice unpacked size: 253 B                                   
npm notice shasum:        114391f7d19b7f9f203100fdb5f68a3159054029
npm notice integrity:     sha512-lbWsQQJkLAbaC[...]R3PvwVxXnGIRQ==
npm notice total files:   2                                       
npm notice 


however, git won’t commit assets/node_modules/a.js

Platform Info

$ npm --versions
<!-- paste output here -->
{ npm: '6.7.0',
  ares: '1.10.1-DEV',
  cldr: '32.0',
  http_parser: '2.8.0',
  icu: '60.1',
  modules: '57',
  napi: '3',
  nghttp2: '1.32.0',
  node: '8.11.3',
  openssl: '1.0.2o',
  tz: '2017c',
  unicode: '10.0',
  uv: '1.19.1',
  v8: '6.2.414.54',
  zlib: '1.2.11' }
$ node -p process.platform
<!-- paste output here -->

I was not able to reproduce this, when I tried assets/node_modules/a.js was excluded from the pack as expected.

Do you have a files entry in your package.json? This is used if present as a whitelist instead of the implicit blacklist from .gitignore.



λ   cat package.json 
  "name": "npm-ignore",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "author": "",
  "license": "ISC"

And please notice that there is a slash at the end of node_modules/ in the .gitignore, if I remove the slash, it works as expected.

I did miss the trailing slash, and have now reproduced. Also on Mac.

I wondered if the trailing slash wasn’t included in npmignore rules but is is included explicitly in Keeping files out of your package (as well in the .gitignore docs)

  • You can end patterns with a forward slash / to specify a directory.

Same thing by replacing node_modules with foo, it’s not specific to the node_modules rules. foo/ just doesn’t seem to work when foo is a subdirectory, and I don’t know if that’s intended.

A work-around to get a directory excluded at any level is to include leading **/: