npm publish a package with only the children of dist folder [duplicate]
Asked Answered
A

1

14

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/"
]
Addie answered 6/1, 2018 at 6:10 Comment(0)
D
23

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

Drama answered 26/1, 2018 at 10:59 Comment(2)
Thank you! This should be documented on npm's docsSwob
This did not work for me, though programmatically changing the outDir entry in my tsconfig.json from ./dist/ to ./ did the trick!Dabber

© 2022 - 2024 — McMap. All rights reserved.