Search code examples
node.jsnpmpackage.jsonnpm-publish

npm publish a package with only the children of dist folder


My package is structured as follows:

mypackage
   |--- src
   |     |--- component1
   |     `--- component2
   `--- dist
         |--- component1
         `--- component2

When I publish it to npm, I would like it to look like the following, without the dist directory:

mypackage
   |--- component1
   `--- component2

The idea is that when I import from this package, the imports should look file this:

import component1 from 'mypackage/component1'

an not this (notice the extra dist):

import component1 from 'mypackage/dist/component1'

How to achieve this? I currently have a files section in my package.json which publishes with the extra dist and I don't want that:

"files": [
  "dist/"
]

Solution

  • Based on the answer from How to publish contents only of a specific folder? this may be of use:

    npm run build
    cp package.json ./dist
    # or, if you need to have package.json "main" entry different,
    # e.g. for being able to use `npm link`, you need to replace "main" 
    value:
    # sed 's#"main": "./dist/index.js"#"main": "./index.js"#' package.json > ./dist/package.json
    cd ./dist
    npm publish
    

    So just basically copy your package.json into the dist/ folder and then run npm publish from within the dist/ folder