Publishing to npm, file defined in `main` is not published

Viewed 827

I'm trying to publish a package to npm and for some reason the .js I've defined in the package.json isn't included with the package. My project is written in typescript and I transpile via the following npm script...

"prepublishOnly": "tsc -p ./ --outDir dist/"

Which correctly outputs the file I expect to dist/app.js locally. I then do an npm publish and the readme.md and some other files are published, but not the app.js I expect to be. The package.json has the main set as dist/app.js so what am I missing here?

This is my first npm package publish, so it's probably something stupid, happy to try any ideas.

1 Answers

The issue is most likely because you have a .gitignore file, which npm will respect as a fallback if no .npmignore file is found. I am guessing you are running into the above, perhaps ignoring /dist in source control?

If there's no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file

main will not actually dictate what content is packed...

The main field is a module ID that is the primary entry point to your program. That is, if your package is named foo, and a user installs it, and then does require("foo"), then your main module's exports object will be returned.

Additionally, packaged files can explicitly included via files

"files": ["dist/app.js", "/and/others/"]

And lastly, if you take a peek at npm/issues/3571, you can workaround this by simply adding an empty .npmignore file.

If you want to include something that is excluded by your .gitignore file, you can create an empty .npmignore file to override it.


pro tip - you can run npm pack locally, which will execute all the lifecycle hooks of a publish and provide you the .tgz package as if it were fetched from npm. You can unzip this to inspect the contents. I've found this super useful on many occasions to emulate publishing without actually doing so.

Related