Collin
Collin

Reputation: 25

When publishing a nodejs package, some files are upoload to npm but not upload to Github. Why?

When publishing a nodejs package, some files are uploaded to npm but not uploaded to Github. Why?

For example, the package is assets-webpack-plugin.

In the files section of its package.json it includes dist/:

"files": [
  "index.js",
  "dist/"
],

But in the .gitignore file it still includes dist:

node_modules
dist
tmp
npm-debug.log*
.package.json

# Lock files shouldn't be committed for libraries https://stackoverflow.com/a/40206145
yarn.lock
package-lock.json

The question is why does the project on Github ignore the files while npm has to contain them? Now that they are in package.json's files section, which means these file(s) are important to the package - why .gitignore them?

Upvotes: 1

Views: 740

Answers (1)

RobC
RobC

Reputation: 25002

Let's firstly understand some more about the "build" and "publish" steps for this project...

If you inspect the projects package.json file it contains the following two scripts defined in the scripts section that are pertinent to your question:

"scripts": {
    ...
    "build": "babel index.js --out-dir dist && babel lib --out-dir dist/lib",
    "prepublish": "npm run build"
},

When the maintainers of assets-webpack-plugin publish the package to npm they utilize the npm publish command.

Upon running npm publish ... the following steps occur:

  1. The prepublish script defined in the package.json (which is known as a pre hook) automatically gets invoked and it runs the build script.

  2. The build script essentially utilizes the babel-cli tool to begin the process of transpiling the projects source code (i.e. the source code is what you see in the projects GitHub repo). The resultant transpiled code is what gets published to npm.

    Note: in the build script the output file(s) are saved to the dist and dist/lib folders.

The aforementioned steps (1 and 2) occur before the npm publish ... command publishes the package to npm.


The question is why does the project on Github ignore the files while npm has to contain them?

  1. Let's address the "why does the project on Github ignore the files" part of your question:

    • The files which are saved to the dist folder are automatically generated from the source code, as per the aforementioned steps 1 and 2.

    • The process of generating the "build" file(s) is repeatable. The resultant file output from this "build" process are derivatives of the source code files.

    • Essentially, there is no reason to push the "build" files, (i.e. the files that are automatically generated to the dist folder), to the GitHub repo because the project maintainer(s) know that they can simply regenerate the derivative versions from their source code by running the npm build script.

  2. Let's try to address the "... while npm has to contain them" part of your question:

    • npm has to contain the files in the dist folder because they are essentially the files that get published to the npm registry.

Upvotes: 1

Related Questions