Search code examples
gitnpmrepository

Hide folders with npm


Is it possible not to make available all folders with npm ?

I have a git repository with :

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

File package.json is containing :

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

And a have a second repository containing a package.json file :

{
   "name": "myRepo2",
   "repository": {
     "type": "git",
     "url": "https://somewhereelse"
   },
   dependencies": {
     "myRepo": "git+ssh:https://git@somewhere"
   }
   ...
}

But nodemodules in myRepo2 is containing myRepos/FolderToHide. How can I configure myRepo to make available only some folders ?


Solution

  • you can use .npmignore to control the files and folders which will be installed when another repository installs your package via npm. Check the difference of .npmignore and .gitignore from this .

    1. Create the .npmignore file in the root folder of myRepo.

    2. Inside the file, paste this FolderToHide/ and save it.

    3. Now update the package.json myRepo to whitelist the folders you want to publish.

        `        "files": [
                    "library/"
                  ]`
      
    4. Now your myRepo2 can download and install the updated npm packages from myRepo

    Refer this also for a better understanding.

    https://docs.npmjs.com/cli/v9/using-npm/developers#keeping-files-out-of-your-package