GithubHelp home page GithubHelp logo

Comments (5)

rdmurphy avatar rdmurphy commented on May 7, 2024 1

Sure! I'll try to take a swing at it this weekend.

from git-hooks.

Qix- avatar Qix- commented on May 7, 2024

Yeah, not sure why I didn't add the extension. Does .js work? I've personally never seen .cjs, is that a new thing?

from git-hooks.

rdmurphy avatar rdmurphy commented on May 7, 2024

Yeah! It's part of Node's move to supporting ESM modules while transitioning away from CommonJS. It's a bit of a mess. 😅

Once Node v10 goes end-of-life in April, all supported versions of Node (starting with Node v12 LTS) will support this new dual system. .cjs is one of a few ways users can tell Node to treat a file as CommonJS. (With .mjs doing the same for ES Modules.)

I did try to add .js, but the issue remained because due to my package.json having "type": "module" set, it assumes it is also an ES Module file and tries to load it as one. With the .cjs extension it overrides my setting and will load it as CommonJS as it expects.

I think normally this isn't a problem because Node smooths this over with files in node_modules (which have their own package.json that tell Node now to treat each of its files), but git hooks are in a tricky place because they effectively run within the scope of the project using them.

I didn't try it, but if the .git/hooks folder wouldn't get mad about having a file called package.json in it, one possible solution is putting this in there:

{
 "private": true,
 "type": "commonjs"
}

Kind of weird, but should be enough to tell Node to treat all files within that folder as CommonJS.

Another option is potentially doing a breaking change/major release to commit to the .cjs extension (or rewrite to use .mjs) so it's compatible with all environments it may get used in going forward.

from git-hooks.

Qix- avatar Qix- commented on May 7, 2024

Using the .cjs extension wouldn't be a breaking change I don't think. Previously, Node didn't care about the extension when passing in paths from the command line, which is what we're doing now. Adding .cjs shouldn't break anyone, I don't think.

Anyway, it might take me a while to get to this; if you wanted to open a PR, I would review it ASAP and release a fix :) Thanks for opening the ticket.

from git-hooks.

Qix- avatar Qix- commented on May 7, 2024

Awesome :)

from git-hooks.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.