Search code examples
npmgitignorenpm-publish

npm publish .gitignored file


The situation is the following

I have a .gitignore file:

node_modules
npm-debug.log
/index.js # this is a build output, I don't want it in the repo

And a package.json file (just the relevant parts):

{
  "main": "index.js",
  "files": [
    "index.js", // however I want this in the npm package
    "readme.md",
    "package.json"
  ],
  "scripts": {
    "build": "rollup -c rollup.config.js", // this builds index.js ...
    "lint": "eslint **/*.js --config .eslintrc",
    "test": "jest --no-cache",
    "prepublish": "npm run lint && npm test && npm run build" // ...before publishing
  }
}

When I published the first version of this, index.js was omitted, and only readme.md and package.json were properly published. The only thing I can assume is that .gitignore causes this, since according to the docs .gitignore substitutes .npmignore if the later is not present (or maybe the 2 are used together, no idea).

So is there a way to not have that file in the git repo, but have it in the npm package?


Solution

  • Managed to solve this by adding a .npmignore file:

    # all the crap that's not neccessary for the npm module to work
    node_modules
    src
    test
    .babelrc
    .eslintignore
    .eslintrc
    .gitignore
    .npmignore
    .travis.yml
    rollup.config.js
    

    And removing the files attribute completely from package.json.