Create two version for a package

69 views Asked by At

Is it possible provide two version of a same package npm ?

I have a git repository with :

MyRepo
|- library
|- FolderToHide
|- package.json
|- .npmignore

File package.json is containing :

{
   "name": "myRepo",
   "repository": {
     "type": "git",
     "url": "https://somewhere"
   },
   ...
}

How can I configure my package to create a version with FolderToHide and another without ?

I want to have something like this :

MyRepo2
|- node_modules
  |- myRepo
    |- library

MyRepo3
|- node_modules
  |- myRepo
    |- library
    |- FolderToHide

MyRepo3 can be used as debug repository, maybe is it possible to create a debug version for a package ?

2

There are 2 answers

0
OlegWock On BEST ANSWER

If you want it to be single npm package, e.g. my-package (not two separate packages my-package and my-package-with-hidden-folder) you can use dist-tags for this.

  1. When you made changes to your package which you want to publish, add FolderToHide to your .npmignore.
  2. Add -limited to version of your package in package.json (so it looks like 2.1.0-limited) and run npm publish --tag limited (you can replace limited with any name you want). This will publish version of package without FolderToHide with tag limited.
  3. Remove FolderToHide from .npmignore
  4. Change -limited in package.json to -full and run npm publish --tag full (again, full can be any name you wish). This will publish new version of package with FolderToHide included.

Then, when you want to install your package in another project you run either npm install my-package@limited or npm install my-package@full.

Alternatively, you can decide to have one of versions as default option. In this case you should omit dist-tag for it. So instead of npm publish --tag full you just run npm publish. This will (implicitly) assign tag latest to published version. And when you install package without specifying tag (npm install my-package), npm will use package with latest tag.

0
phun-ky On

If I understand correctly, you want to publish two different packages to npm (or similar), but do not want 2 "repositories"?

Ok, first, create a node-script in the root of your package that you for example call "publish.js".

In that script you put something like this (pseudo code):

const fs = require('fs');
const { spawn } = require('node:child_process');

let packageJSON = fs.readFileSync("path to package.json");

// For the first package,without the hiddenfolder:
packageJSON.files = ["library", "package.json", "any  other file you want to include in the package"];

fs.writeFileSync("path to the project package.json", JSON.stringify(packageJSON, null, 2), 'utf-8');

// then run publish (version first if pref)

const publish = spawn('npm', ['publish']);

publish.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

publish.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

// For the second package,with the hiddenfolder:
packageJSON = fs.readFileSync("path to package.json");
packageJSON.files = ["library", "hiddenFolder","package.json", "any  other file you want to include in the package"];

fs.writeFileSync("path to the project package.json", JSON.stringify(packageJSON, null, 2), 'utf-8');


// then run publish (version first if pref)

const publish = spawn('npm', ['publish']);

publish.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

publish.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

Then add a script tag in your package.json:

{
  …
  "scripts": {
    …
    "release": "node ./publish.js",
    …
  }
  …
}

And so, every time you want to release, just run npm run release. npm pack will only pack the contents of what is in the files array. Click here for full documentation regarding files

NOTE This is just unproven pseudo code, YMMV.

One other option is to copy to a temp directory (hidden by both .npmignore and .gitignore including a modified package.json that contains files that you want to release, and run npm publish from that directory.