How to npm publish specific folder but as package root

后端 未结 10 1287
后悔当初
后悔当初 2020-12-13 01:10

I have a project that include a gulp task for building and packaging the sources and release in a directory called dist. My goal is publish it as a npm package,

10条回答
  •  渐次进展
    2020-12-13 01:52

    You need to publish the dist folder

    The natural way to achieve that, according to the npm approach, is to publish the folder which is to be the root. There are several ways to do that, depends on the final environment you want to work with:

    1. npm publish from your package repo to an npm registry and then install your package in other project as you install other packages. In your case it would be npm publish dist.
    2. npm install in some other project if you want to use your package only locally. In your case you go to the other project and run npm install relative/path/to/dist
    3. npm link your folder locally to your node_modules in another project in case you'd like to have the changes in your original package reflected instantly in other project. In your case you first cd dist and run npm link and then go to the other project and run npm link robsonrosa-ui-alert.

    Prerequisite: in any case above, prior to the publish/install/link, you have to put in your dist folder at least a proper package.json file. In your case, you have to have the package name defined in your package.json file as "name": "robsonrosa-ui-alert". Typically, you'll want there also some other files like README.md or LICENSE.

    Automation example

    You can automate the publishing process using the prepare script, combined with build script. Additionally, you can protect your package against an accidental publishing of the package root folder with "private": true field put in the package.json, located in the root directory of your package repo. Here is an example:

      "private": true,
      "scripts": {
        "build": "rm -rf dist && gulp build && cat ./package.json | grep -v '\"private\":' > dist/package.json",
        "prepare": "npm run build"
      },
    

    This way you won't publish the root folder and get the package built and package.json copied to the dist folder automatically, within the publishing process.

提交回复
热议问题