How to publish an npm package with distribution files?

node.jsGitGithubNpm

node.js Problem Overview


I would like to publish a npm package that contains my source as well as distribution files. My Github repository contains src folder which contains JavaScript source files. The build process generates dist folder that contains the distribution files. Of course, the dist folder is not checked into the Github repository.

How do I publish a npm package in a way that when someone does npm install, they get src as well as dist folder? Currently when I run npm publish from my git repository, it results in only src folder being published.

My package.json looks like this:

{
  "name": "join-js",
  "version": "0.0.1",
  "homepage": "https://github.com/archfirst/joinjs",
  "repository": {
    "type": "git",
    "url": "https://github.com/archfirst/joinjs.git"
  },
  "main": "dist/index.js",
  "scripts": {
    "test": "gulp",
    "build": "gulp build",
    "prepublish": "npm run build"
  },
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  }
}

node.js Solutions


Solution 1 - node.js

When you npm publish, if you don't have an .npmignore file, npm will use your .gitignore file (in your case you excluded the dist folder).

To solve your problem, create a .npmignore file based on your .gitignore file, without ignoring the dist folder.

Soure : https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package

Solution 2 - node.js

Take a look at the "files" field of package.json file https://docs.npmjs.com/files/package.json#files

From the documentation:

> The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder. (Unless they would be ignored by another rule.)

Solution 3 - node.js

Minimal example of how to use data files from a script

Another common use case is to have data files that your scripts need to use.

This can be done easily by using the techniques mentioned at: https://stackoverflow.com/questions/10111163/in-node-js-how-can-i-get-the-path-of-a-module-i-have-loaded-via-require-that-is

The full example can be found at:

With this setup, the file mydata.txt gets put into node_modules/cirosantilli-data-files/mydata.txt after installation because we added it to our files: entry of package.json.

Our function myfunc can then find that file and use its contents by using require.resolve. It also just works on the executable ./cirosantilli-data-files of course.

package.json

{
  "bin": {
    "cirosantilli-data-files": "cirosantilli-data-files"
  },
  "license": "MIT",
  "files":  [
    "cirosantilli-data-files",
    "mydata.txt",
    "index.js"
  ],
  "name": "cirosantilli-data-files",
  "repository": "cirosantilli/linux-kernel-module-cheat",
  "version": "0.1.0"
}

mydata.txt

hello world

index.js

const fs = require('fs');
const path = require('path');

function myfunc() {
  const package_path = path.dirname(require.resolve(
    path.join('cirosantilli-data-files', 'package.json')));
  return fs.readFileSync(path.join(package_path, 'mydata.txt'), 'utf-8');
}
exports.myfunc = myfunc;

cirosantilli-data-files

#!/usr/bin/env node
const cirosantilli_data_files = require('cirosantilli-data-files');
console.log(cirosantilli_data_files.myfunc());

The is-installed-globally package is then useful if you want to generate relative paths to the distributed files depending if they are installed locally or globally: https://stackoverflow.com/questions/26104276/how-to-tell-if-npm-package-was-installed-globally-or-locally/60486676#60486676

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNareshView Question on Stackoverflow
Solution 1 - node.jstophemanView Answer on Stackoverflow
Solution 2 - node.jsEugene NezhutaView Answer on Stackoverflow
Solution 3 - node.jsCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow