[DOCS] `npm pack` doesn't include file specified in `main` field
Is there an existing issue for this?
- [X] I have searched the existing issues
This is a CLI Docs Problem, not another kind of Docs Problem.
- [X] This is a CLI Docs Problem.
Description of Problem
The following section of the docs suggest that even with an empty files array, the path from main in package.json would still be included when running npm pack, but it doesn't behave accordingly for me:
https://github.com/npm/cli/blob/6ca609e20b68fb2e5ef8177db116b84a339461fd/docs/lib/content/configuring-npm/package-json.md?plain=1#L296-L302
Potential Solution
If someone can confirm that this is indeed a discrepancy between the docs and the implementation, one should be adjusted to match the other.
Affected URL
No response
It does include main file when files array is empty
~/workarea/rep/test2 $ cat package.json
{
"name": "test2",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"files":[],
"author": "",
"license": "ISC",
"description": ""
}
~/workarea/rep/test2 $ npm pack
npm notice
npm notice 📦 [email protected]
npm notice Tarball Contents
npm notice 21B index.js
npm notice 214B package.json
npm notice Tarball Details
npm notice name: test2
npm notice version: 1.0.0
npm notice filename: test2-1.0.0.tgz
npm notice package size: 289 B
npm notice unpacked size: 235 B
npm notice shasum: eb360f5112d60fbe8bd07966ba034e1b646d3677
npm notice integrity: sha512-dPPp1JvlntDq+[...]FQDURsukkbOzw==
npm notice total files: 2
npm notice
test2-1.0.0.tgz
Can you show the output of npm pack --dry-run when the files array is not empty?
~/workarea/rep/test2 $ cat package.json
{
"name": "test2",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"files":["some-random-file.js"],
"author": "",
"license": "ISC",
"description": ""
}
~/workarea/rep/test2 $ npm pack --dry-run
npm notice
npm notice 📦 [email protected]
npm notice Tarball Contents
npm notice 21B index.js
npm notice 235B package.json
npm notice Tarball Details
npm notice name: test2
npm notice version: 1.0.0
npm notice filename: test2-1.0.0.tgz
npm notice package size: 301 B
npm notice unpacked size: 256 B
npm notice shasum: fea7a90b35ad5b89f5e454561162acc6c55ea122
npm notice integrity: sha512-Fwa3c+JPwwxy9[...]W/iiFfl/UFzkA==
npm notice total files: 2
npm notice
test2-1.0.0.tgz
Looks like index.js is included in both cases.
I did some further testing and figured out that the issue only comes up, when the main field is specified as a relative path, in which case the provided value still works perfectly fine for it's original purpose (that is, specifying the file to be imported when the package is used as a dependency), but stops being included in the distributed package by default.
For example, given the following package:
/ ❯ rg "" .
./pkg1/package.json
1:{
2: "name": "pkg1",
3: "version": "1.0.0",
4: "main": "./example.js",
5: "files": []
6:}
./pkg1/example.js
1:module.exports = { x: 42 };
It works when imported:
/ ❯ mkdir pkg2; cd pkg2
/pkg2 ❯ npm install ../pkg1
added 1 package in 110ms
/pkg2 ❯ node -e "console.log(require('pkg1'))"
{ x: 42 }
But doesn't include the file in the archive:
/pkg2 ❯ cd ../pkg1
/pkg1 ❯ npm pack --dry-run
npm notice
npm notice 📦 [email protected]
npm notice === Tarball Contents ===
npm notice 84B package.json
npm notice === Tarball Details ===
npm notice name: pkg1
npm notice version: 1.0.0
npm notice filename: pkg1-1.0.0.tgz
npm notice package size: 165 B
npm notice unpacked size: 84 B
npm notice shasum: b76b9256d8f14870061f7b9a1d3ef7c7def7099e
npm notice integrity: sha512-iDd4jjnyTHmxu[...]q4z81tFDwGOaQ==
npm notice total files: 1
npm notice
pkg1-1.0.0.tgz