0

I'm working on several internal node modules that I do not want to publish to the NPM registry. Is there a way to do this, in a way that these internal node modules also have their own versioning?

Example:

import { someMethod } from '../../../sample-module'

I am hoping to have some sort of versioning for the sample-module, similar to how it is at the NPM registry. Maybe have a structure that looks like this:

> sample-module
   > 1.0.0
   > 1.0.1
   > 1.0.2

I am looking at Node's subpath imports but that doesn't seem to support module versioning (or does it?) I don't quite understand how it works. The other examples I've seen online doesnt seem to have versioning and is actually somehow used differently. Here's an example from Node (https://nodejs.org/api/packages.html#subpath-imports):

// package.json
{
  "imports": {
    "#dep": {
      "node": "dep-node-native",
      "default": "./dep-polyfill.js"
    }
  },
  "dependencies": {
    "dep-node-native": "^1.0.0"
  }
} 

Best if I can just use the internal module in my app as:

import { someMethod } from 'sample-module'

And have it in my package.json with a proper version.

Any idea how to achieve having local node modules + module versions?

Thanks!

kzaiwo
  • 1,558
  • 1
  • 16
  • 45

1 Answers1

0

You can do the versioning in your module in the package.json with the attribute "version": "x.x.x".

Then add the package/module to your project with the npm-link feature:

For example:

cd ~/projects/node-redis    # go into the package directory
npm link                    # creates global link
cd ~/projects/node-bloggy   # go into some other package directory.
npm link redis              # link-install the package
tom
  • 9,550
  • 6
  • 30
  • 49
  • Is there a way though to import/install the local module and have multiple versions of it? Say, ```sample-module``` (versions 1.0.0, 1.0.1, 1.0.2) exist in my local at the same time and then the consuming app will install the sample-module based on the version listed in the "dependencies" on the consuming app's package.json. I know I can name it like ```sample-module-1.0.0``` or ```sample-module-1.0.1``` or ```sample-module-1.0.2```.. but I'm hoping for a cleaner, less hacky solution – kzaiwo Jul 05 '23 at 10:17
  • package aliases https://stackoverflow.com/questions/26414587/how-to-install-multiple-versions-of-package-using-npm – tom Jul 05 '23 at 10:23