Click here to Skip to main content
15,844,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Issue for - Azure Build pipeline for an angular project.
I have to run the npm install job every time the angular project builds with a new pull request.

Scenario - I have customized 2 packages for my project's requirement. I do not want to override these packages every time there is a npm install command. Once it is overridden manually it should not be touched.

Is there a way to ignore a particular package in package.json once it is installed?
in other words, Is it possible to install a npm package only if it has not been already installed?
Seeking for some direction from experts :)

What I have tried:

1. Exclude these packages from the package.json file

Problem with this solution - If I have to deploy my application on the new system, those packages will never be installed.

2. I added these packages into a different folder and tried to reference the packages from there. It is not working as expected.
Updated 10-Oct-23 8:55am

1 solution

You can try the following;

Using npm-shrinkwrap.json
1. Customize and Install Packages: Initially install and customize your packages as needed.
2. Generate npm-shrinkwrap.json: Run npm shrinkwrap in your project directory. This command creates an npm-shrinkwrap.json file which locks down the versions of all installed packages and their dependencies.
3. Edit npm-shrinkwrap.json: In the npm-shrinkwrap.json, you can manually edit the entries for your customized packages to point to your specific versions or local copies. This way, whenever npm install runs, it respects the versions and sources specified in the npm-shrinkwrap.json, ignoring any updates or changes that would normally be fetched from the npm registry.

If that doesn't work, try with local paths:

Leveraging package-lock.json with Local Paths
1. Install Custom Packages Locally: Place your customized packages in a known directory within your project or somewhere accessible to your project.
2. Modify package.json: Reference these local packages in your package.json by specifying their paths instead of version numbers.
"dependencies": {
  "custom-package": "file:./path/to/custom-package",

3.Commit package-lock.json: After running npm install, ensure that your package-lock.json is updated and commit this file to your repository. The package-lock.json ensures that the exact structure of your node_modules directory is replicated on future installations, including the use of local packages.

I would advise to use the first to methodes, if that doesn't work you can go and override packages.

Using .npmrc for Overriding Package Locations
Configure .npmrc: You can use an .npmrc file to override the source location of specific packages. This is a bit more advanced and typically used to switch between different registries, but you can specify local paths or custom URLs for your packages.

However, this approach requires that your packages are structured and can be installed in a manner similar to those hosted on npm registries, which might involve additional setup for your custom packages.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900