npm pack/publish does not pack files ignored by inner package.json


(Gabriel Montes) #1

What I Wanted to Do

I would like to publish a package that contains a repository template inside to use with npm init and initialize a new fully-functional repository. The template folder contains dotfiles, source files and a package.json that specifies what files have to be included when packaging the initialized repo files through the files property.

What Happened Instead

Some files in the template folder are not packed in the tarball when published as npm publish not only follows the include rules of the package.json but also the inner template/package.json for that folder.

Reproduction Steps

Clone https://github.com/gabmontes/npm-publish-test to have a repository with the following structure:

β”œβ”€β”€ index.js
β”œβ”€β”€ package.json
└── template
    β”œβ”€β”€ package.json
    β”œβ”€β”€ src
    β”‚   └── index.js
    └── test
        └── index.js

The root package.json has no files property so all files should be included in the package when publishing. The inner template/package.json files does have files: ['src'] so only source files are published when using the template.

Then run npm publish --dry-run to check what is actually packed:

npm notice === Tarball Contents === 
npm notice 55B package.json         
npm notice 14B index.js             
npm notice 23B template/src/index.js
npm notice === Tarball Details === 

Instead of including all files under template as the topmost package.json says, npm publish follows the rules of the package.json inside the template folder to process the files in that folder, preventing the publication of other template files as intended.

Platform Info

$ npm --versions
{ 'npm-publish-test': '1.0.0',
  npm: '6.4.1',
  ares: '1.10.1-DEV',
  cldr: '32.0',
  http_parser: '2.8.0',
  icu: '60.1',
  modules: '57',
  napi: '3',
  nghttp2: '1.33.0',
  node: '8.15.0',
  openssl: '1.0.2q',
  tz: '2017c',
  unicode: '10.0',
  uv: '1.23.2',
  v8: '6.2.414.75',
  zlib: '1.2.11' }
$ node -p process.platform
darwin

(Lars Willighagen) #2

This is poorly documented but (possibly?) expected behavior (there’s a test case for it, specifically). I’m trying to figure out if it really is intended, or just a side-effect of bundledDependencies code, but I don’t know how long that’ll take.

A workaround for this particular use-case would be renaming template/package.json and moving it after copying template/, or adding something to the file so that it doesn’t parse as JSON and removing it later – whichever works better.