Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS] npm pack doesn't include file specified in main field #7799

Open
2 tasks done
isti115 opened this issue Oct 1, 2024 · 5 comments · May be fixed by npm/package-json#136
Open
2 tasks done

[DOCS] npm pack doesn't include file specified in main field #7799

isti115 opened this issue Oct 1, 2024 · 5 comments · May be fixed by npm/package-json#136
Assignees
Labels
Bug thing that needs fixing Priority 2 secondary priority issue

Comments

@isti115
Copy link

isti115 commented Oct 1, 2024

Is there an existing issue for this?

  • I have searched the existing issues

This is a CLI Docs Problem, not another kind of Docs Problem.

  • 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:

Certain files are always included, regardless of settings:
* `package.json`
* `README`
* `LICENSE` / `LICENCE`
* The file in the "main" field
* The file(s) in the "bin" field

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

@isti115 isti115 added Documentation documentation related issue Needs Triage needs review for next steps labels Oct 1, 2024
@milaninfy
Copy link
Contributor

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 📦  test2@1.0.0
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

@ljharb
Copy link
Contributor

ljharb commented Oct 1, 2024

Can you show the output of npm pack --dry-run when the files array is not empty?

@milaninfy
Copy link
Contributor

~/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 📦  test2@1.0.0
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

@ljharb
Copy link
Contributor

ljharb commented Oct 1, 2024

Looks like index.js is included in both cases.

@isti115
Copy link
Author

isti115 commented Oct 2, 2024

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 📦  pkg1@1.0.0
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

@milaninfy milaninfy added Priority 2 secondary priority issue Bug thing that needs fixing and removed Needs Triage needs review for next steps Documentation documentation related issue labels Oct 3, 2024
@milaninfy milaninfy self-assigned this Dec 6, 2024
@milaninfy milaninfy linked a pull request Dec 9, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Priority 2 secondary priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants