Back

Technologies:

javascriptjavascript
node.jsnode.js
npmnpm
avatar
Tolerim
22 days ago

What is the process for publishing my private npm package, including node modules, to a private repository?

Issue:

The nodejs code has private dependencies located in various folders with relative paths specified in the package.json file of project A, which has dependencies B and C. Prior to building A, B and C must be built, with the latter being dependent on the former. While building C, B's nodemodules are essential. I am attempting to automate this process using Jenkins, but cannot use relative paths and require these dependencies to be hosted on private repositories, such as Nexus Artifacts Repository. However, C requires nodemodules, which are not included in the published package.

Attempts:

I have attempted to publish packages on Sonatype Nexus Repository, but they do not contain nodemodules.

Answers(2)
avatar
Vogi
22 days ago
Verified Answer
To publish nodejs packages with nodemodules on Nexus Artifacts Repository, you can create a proxy and a group repository. Then you can delete the node_modules and package-lock.json in your project and run npm install. Because your hosted don't have those packages,it will download them from proxy. Then you can see those packages in the group. When you run npm install again,it will download form Nexus ,not NPM. You can also use Sonatype Nexus Repository to host your private dependencies. You can add your customized logic inside the Index.js file and for package.js add the following:
"name": "my-package",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "publishConfig": {
    "registry": "http://your-host:8081/repository/npm-private/"
  }
}
You can then publish your package using npm publish. For more information on how to integrate Nexus-hosted npm registry into your Node.js toolchain, you can check out this IBM Cloud blog post.

Sources

Uploading node_modules to Nexus hosted NPM repositoryCustom Node Module Management using Private npm Registry ... - SonatypeIntegrate your Nexus-hosted npm registry into your Node.js toolchainUsing Sonatype Nexus Repository 3 – Part 2: npm Packagesnpm - Upload node modules to nexus - Stack Overflow
avatar
Tolerim
22 days ago
To solve this problem, you can create separate packages for B and C using a package manager like npm, and then publish them to your private repository like Sonatype Nexus. You can also specify the dependencies of C in the package.json file using the package names of B and C instead of using relative paths. To ensure that the node_modules dependencies are included in the published packages, you can add a "prepare" script to the package.json of B and C. This script will run automatically after npm install and will ensure that the required dependencies are installed and available in the package. Here is an example of how the package.json file of B should look like:
{
  "name": "B",
  "version": "1.0.0",
  "dependencies": {
    // dependencies of B
  },
  "scripts": {
    "prepare": "npm install"
  }
}
Similarly, the package.json file of C should look like:
{
  "name": "C",
  "version": "1.0.0",
  "dependencies": {
    "B": "1.0.0" // dependency on B package
    // other dependencies of C
  },
  "scripts": {
    "prepare": "npm install"
  }
}
Once you have published B and C packages to your private repository, you can install them in Project A using the npm install command. This will automatically install the required dependencies in the correct order, i.e., first B and then C.
;