Skip to content

*.cjs extension files served as text/plain #355

Closed

Description

@Swivelgames

Modern Node.js modules can be served as ESMs or CommonJS modules. These are differentiated either by their package.json, or by the extension of the file. It appears that unpkg properly serves .mjs files, but seems to treat *.cjs files as plaintext incorrectly.

Instead of text/plain, the content-type of .cjs files should be application/javascript.

Node's documentation can serve as reference for the honoring of these files: https://nodejs.org/api/modules.html#enabling

Activity

  1. Potherca commented on May 10, 2023

    @Potherca

    Running into this as well. Looks like this issue has been reported in 2020 in #268.
    Would be nice to have as this basically makes UNPKG unusable for a large part of the JS ecosystem. 😞

  2. Potherca commented on May 10, 2023

    @Potherca

    If I understand correctly (based on #65), the content-type comes from the mime package?

    It is currently pinned at 2.4.*

    https://github.com/mjackson/unpkg/blob/af8c8db00fdacd77961ab2a8c3edb45a27d3a6a3/package.json#L35

    The cjs was added in v2.4.5 as application/node, so it should work.

    Not sure why it doesn't. What am I missing?

  3. Potherca commented on May 10, 2023

    @Potherca

    As a workaround, I am using the ?module parameter to get the correct content-type.

  4. benmccann commented on Jun 8, 2023

    @benmccann

    The cjs was added in v2.4.5 as application/node, so it should work.
    Not sure why it doesn't. What am I missing?

    Looks like the lockfile needs to be updated:

    https://github.com/mjackson/unpkg/blob/af8c8db00fdacd77961ab2a8c3edb45a27d3a6a3/yarn.lock#L4620

  5. maximedupre commented on Aug 7, 2023

    @maximedupre

    #364 sounds like an easy merge 🥹

    Can we get this done?

  6. MedievalMatt commented on Feb 17, 2024

    @MedievalMatt

    This is still an issue as of February 2024. unpkg.com/three (direct link https://unpkg.com/three@0.161.0/build/three.cjs) throws up a nosniffer error and is being read as "text/plain."

  7. benmccann commented on Feb 17, 2024

    @benmccann

    The cjs was added in v2.4.5 as application/node, so it should work.

    I was working off of this statement when I sent the PR, but am not sure it's accurate. I don't think Chrome will treat application/node the same as application/javascript. The PR should still be merged so that .cjs files will be served according to the spec, but I'm not so sure it's actually going to help anyone

  8. brianlagunas commented on Jun 24, 2024

    @brianlagunas

    This is still an issue. As you can see with this packages: https://unpkg.com/browse/@revealbi/ui@0.2.0/

    The .cjs files are marked as text/plain and will load not as javascript using a script tag

    <script src="https://unpkg.com/@revealbi/ui@0.2.0/index.umd.cjs"></script>
    
  9. Swivelgames commented on Jul 25, 2024

    @Swivelgames
    Author

    @brianlagunas Unfortunately, it looks like unpkg hasn't been touched for about 3 years. I'm starting to lose hope, unfortunately.

    It doesn't look like the SPONSORS.md file has been updated for even longer suggesting that, despite its popularity, it might not have the funding necessary to continue to be maintained.

    This is all purely speculation, of course, and I hope that the project is updated and maintained going forward!

    #364 was opened about a year ago and it was approved a couple months later, but there's been no movement since.

  10. Swivelgames commented on Jul 25, 2024

    @Swivelgames
    Author

    @Potherca described a workaround in the other issue (as well as above) for the time-being. Appending the .cjs URL with ?module apparently forces the correct MIME type.

  11. mjackson commented on Mar 19, 2025

    @mjackson
    Member

    I changed this in b75b59f. .cjs files are now served as text/javascript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions