159
votes

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": {
    ...
  }
}
3

3 Answers

125
votes

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.)

206
votes

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

0
votes

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: In node.JS how can I get the path of a module I have loaded via require that is *not* mine (i.e. in some node_module)

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: How to tell if npm package was installed globally or locally