Search code examples
npmmarkdown

Relative link from readme.md to another file in package, rendered in npmjs


I've created an npm module which is hosted on npmjs.com.

Due to the structure of the module, I've split the documentation into a general readme.md and then more specific files which are linked to from the main readme.md:

.
├──documentation
│   ├── doc1.md
│   └── doc2.md
└── readme.md

The links in readme.md look like this: [some text](documentation/doc1.md). They work perfectly fine locally and in their CodeCommit repo (and I suspect it would be fine in github as well).

However after publishing it to npm, the links become https://www.npmjs.com/package/documentation/doc1.md, which of course is an invalid URL and leads to a 404 error.

I've tried to figure out how to get a valid url to the file in question so I can create links that lead to it, but I can't even find out how to get a direct link to the file in the package. It may also be worth noting that the package itself is a private organisation package, but I've tried the same thing on a test public package and had the same issue there, so I don't think it's relevant.

Does anyone know how to link from the root readme.md file to another file in the same package, in a way that works in npmjs?


Solution

  • I've found the issue at npm community they say that it's solved, so I tried for my self to see what went wrong?

    And they were right, I've missed some points so I'll write all the required steps:

    1. Make sure your repository at github/gitlab is public
    2. Make sure in your package.json file you have the proper repository configuration that point at your repo correctly

    Example:

    {
      ...
      "repository": {
        "url": "https://github.com/lalosh/js-library-boilerplate",
        "type": "git"
      }
      ...
    }
    
    1. When doing the relative linking you need to include the relative path from your repo root, including the .md suffix.

    Example:

    We can [link](documentation/sub-doc.md) to an inner relative file