callum
callum

Reputation: 37739

npm publish isn't including all my files

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:

What else could be blocking them?

Upvotes: 51

Views: 27236

Answers (8)

lefrost
lefrost

Reputation: 581

In my case, I needed npm to publish my package with a specific sub-directory, let's say /pkg.

Despite configuring files: ["pkg"] in my package.json, having /pkg specified in my .gitignore caused it to not be included by npm.

I had to remove /pkg from my .gitignore.

Upvotes: 0

In my case, I forgot to add the modules and components to src/public-api.ts

I only added the first one, which imports a second one, so my library only published the two and forgot to do the same for the other 5 or 6 components I had at the time

The reason I didn't notice it until I published it, is that I have my test app as a second project in the same Angular workspace. The test app was importing directly from the library code in the same workspace instead of the dist folder like it should. It didn't find in dist, so the auto-import took it from the project code directly.

Upvotes: 0

callum
callum

Reputation: 37739

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.

Upvotes: 63

Sasi Kumar M
Sasi Kumar M

Reputation: 2630

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."

Upvotes: 10

cdaringe
cdaringe

Reputation: 1538

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

Upvotes: 1

light24bulbs
light24bulbs

Reputation: 3151

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.

Upvotes: 32

Christian Hartmann
Christian Hartmann

Reputation: 661

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

Upvotes: 45

Samuel B. Johnson
Samuel B. Johnson

Reputation: 129

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.

Upvotes: 1

Related Questions