44

I npm publish'd a module. It went up fine, but then when I installed it from the registry, it turned out to be missing certain files.

When I run irish-pub in my module's project directory, sure enough, the output doesn't list those filenames.

I've checked:

  • I do not have an .npmignore file.
  • I do have a .gitignore but this only contains /node_modules/
  • the missing files are normal JS files, not things that could be ignored by default

What else could be blocking them?

callum
  • 34,206
  • 35
  • 106
  • 163

6 Answers6

54

The problem was I had a files array in package.json. If files is present, then only the specified files get published, and everything else is effectively npmignored.

https://docs.npmjs.com/cli/v9/configuring-npm/package-json#files

Additionally: be aware that when there is no .npmignore file and the files array is empty the .gitignore file will be used. From the docs:

You can also provide a .npmignore file in the root of your package or in subdirectories, which will keep files from being included. At the root of your package it will not override the "files" field, but in subdirectories it will. The .npmignore file works just like a .gitignore. If there is a .gitignore file, and .npmignore is missing, .gitignore's contents will be used instead.

Since .gitignore probably ignores your dist folder, this may be causing problems.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
callum
  • 34,206
  • 35
  • 106
  • 163
  • 8
    For anyone else who has this problem, be aware that when there is no .npmignore file and the files array is empty the .gitignore file will be used. – Szenis Jan 10 '19 at 09:44
  • for me is ignoring the files key in package.json and publishing anything !! – Kristi Jorgji Mar 28 '22 at 14:09
  • 1
    For me, I had `src` in my `.npmignore` file, but because I was using TypeScript project references, the `dist` folder had `src` folders inside of it that were also getting ignored. – AlexMorley-Finch Feb 02 '23 at 13:23
  • 1
    I had a similar problem and what I found out is that it npm version 9 takes into account nested .gitignore files (it just ignores the root-level .gitignore when you have a package.json with files). Also, note that alongside the nested .gitignore we have a nested package.json with files array as well. With npm version 8 it was working fine – Gastón Fournier Apr 18 '23 at 13:29
44

i've had the "files" property in package.json as well (intentionaly) but used relative pathes to the list of files and directories starting with dot slash ("./"), but neither npm pack nor npm publish worked with that. removed these, all worked as expected!

so change:

"files": [ "./bin", "./lib" ]

to:

"files": [ "bin", "lib" ]

and run:

$ npm pack

check the gnu zipped tarfile and finaly:

$ npm publish <projectname>-<semver>.tgz
31

For anyone not fixed by the above answers, npm pack and publish respect any package.json files you have in the directory tree, not just at the root.

In my case, I had a module that templated out another module with ejs, but because npm was treating the child package.json as real it was reading the files from there and not bundling everything I needed.

Lookout for the files in any package.json, not just your root.

p1100i
  • 3,710
  • 2
  • 29
  • 45
light24bulbs
  • 2,871
  • 3
  • 24
  • 33
  • How would you fix this, in case you find other `package.json` files? – martin36 May 06 '19 at 17:38
  • I would remove other `package.json` or redefine subdirectory `files` property from each other `package.json` position. – Lukasz Kruszyna Jun 20 '19 at 09:39
  • I ran into this when compiling typescript. The files along with the package.json were built into a child folder. My quick and dirty hack was to use a unix command to rename "files" in the child's package.json to "_files" which would cause npm to ignore it. Here's the command i used `sed -i 's/"files":/"_files":/' lib/package.json`. I was hoping to use shx (a cross-platform wrapper for unix commands) but seems there's a bug with using sed in child directories: https://github.com/shelljs/shx/issues/165 If it gets fixed just add `shx` before the command and it'll work on all platforms – Crhistian Ramirez Aug 06 '19 at 03:35
  • Im maintaining CLI package which creating example project from files. Your post was very helpful. I had package.json inside of dist folder, as part of example project so as a problems with npm publish I changed filename to pkg.json, so it will not disturb CI and fixed logic to rename pkg.json->package.json on command use – Olexander Korenyuk Jan 17 '20 at 12:11
  • Thanks for mentioning this - it was exactly my issue. Another fun fact - npm pack and npm publish respect any package.json, whereas yarn pack only cares about the top level one. – Jed Richards Nov 15 '21 at 17:51
  • 1
    To bypass this. I removed the files array and use .npmignore with the following: * # exclude all !dist/** # add dist back – Justin Poehnelt Dec 15 '21 at 18:03
  • I don't understand - there are HUNDREDS of unavoidable `package.json` in the `node_modules` folder. How would I avoid those? Or what is meant here? I never had this problem, only now for the first time. I once had the "files" section related version of this problem, but now `lib/ ` is missing when I "npm pack" and I have no "files" section. – Mörre Aug 15 '23 at 14:59
9

For me, having the .gitignore file with files listed in it, inside the package folder to be published was causing the issues.

In general,

"All files in the package directory are included if no local .gitignore or 
 .npmignore file exists. If both files exist and a file is ignored by .gitignore 
 but not by .npmignore then it will be included."
Sasi Kumar M
  • 2,440
  • 1
  • 23
  • 23
1

I just ran into the same problem and found the answer here.

You need include the path to the directory (or tarball) you're trying to publish. While the documentation on npmjs.org doesn't really indicate it, if you run npm help publish you'll get the man page, which shows that the correct usage is actually

npm publish <tarball> [--tag <tag>] npm publish <folder> [--tag <tag>]

I also found that I had to actually type out the path: I couldn't just use npm publish . from the directory containing my package.json file.

Hope that helps.

  • 4
    I've never had to specify a path. I just type `npm publish` from inside the directory and it publishes. – callum May 17 '15 at 14:15
1

Something not mentioned in other solutions is that there is an undocumented, racing precedence. For instance, I had "files": ["lib"] in my package.json. lib is my gitignore. with just that state, it works. however, there was also a lib/path/.gitignore, which trumped my files array, yielding no included lib folder.

lesson--take heed of nested .gitignore files

cdaringe
  • 1,274
  • 2
  • 15
  • 33