Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module "@commitlint/config-conventional" when running commintlint globally #613

Open
2 tasks done
dexpota opened this issue Apr 4, 2019 · 47 comments
Open
2 tasks done
Assignees
Labels

Comments

@dexpota
Copy link

dexpota commented Apr 4, 2019

Expected Behavior

I installed commitlint and commitlint/config-conventional globally, thus running echo 'hello world' | commitlint under any directory should works.

Current Behavior

The command raises an exception.

/usr/local/lib/node_modules/@commitlint/cli/lib/cli.js:67
                throw err;
                ^

Error: Cannot find module "@commitlint/config-conventional" from "/home/fabrizio"
    at resolveId (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:134:12)
    at resolveConfig (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:111:18)
    at /usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:63:18
    at Array.reduce (<anonymous>)
    at loadExtends (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:61:32)
    at resolveExtends (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:44:17)
    at Object.$If_1 (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/core/lib/load.js:90:46)
    at Object.<anonymous> (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/core/lib/load.js:159:18)
    at <anonymous>

Affected packages

  • cli
  • config-conventional

Steps to Reproduce (for bugs)

  1. npm install -g @commitlint/cli @commitlint/config-conventional
  2. echo "module.exports = {extends: ['@commitlint/config-conventional']};" > ~/.commitlintrc.js
  3. echo 'should fail' | commitlint
commitlint.config.js ```js module.exports = {extends: ['@commitlint/config-conventional']}; ```

Your Environment

Executable Version
commitlint --version 7.5.2
git --version 2.17.1
node --version 8.10.0
@escapedcat
Copy link
Member

I think commitlint is usually being used on a per project basis. So each project has it's own config.
If you want to use it globally like you tried you could use the --config option and try this:

echo 'should fail' | commitlint --config ~/.commitlintrc.js

@dexpota
Copy link
Author

dexpota commented Apr 5, 2019

I tried your solution and is not working, the same exception as before.

From what I understand from issue #123 it should be possible to use commitlint from any directory starting from version 5.0.1.

@knksmith57
Copy link

we're also running into this issue.

our goal is 0 configuration enforcement of conventional commits in CI --I expected the following to work:

❯ cd $(mktemp -d) && ls -a
.  ..

❯ export CI_COMMIT_MESSAGE='foo: bar'

❯ npx -p @commitlint/config-conventional -p @commitlint/cli -c 'echo $CI_COMMIT_MESSAGE | commitlint -x @commitlint/config-conventional'
npx: installed 137 in 8.51s
/Users/foo/.npm/_npx/6761/lib/node_modules/@commitlint/cli/lib/cli.js:113
	throw err;
	^

Error: Cannot find module "@commitlint/config-conventional" from "/private/var/folders/xr/dxys9d310_s1j3klz_1xwz9jq1qqv5/T/tmp.BsKwQjE1QE"
    at resolveId (/Users/foo/.npm/_npx/6761/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:112:14)
    at resolveConfig (/Users/foo/.npm/_npx/6761/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:92:20)
    at reduce (/Users/foo/.npm/_npx/6761/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:49:20)
    at Array.reduce (<anonymous>)
    at loadExtends (/Users/foo/.npm/_npx/6761/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:47:32)
    at resolveExtends (/Users/foo/.npm/_npx/6761/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:30:19)
    at Object.$If_1 (/Users/foo/.npm/_npx/6761/lib/node_modules/@commitlint/cli/node_modules/@commitlint/load/lib/index.js:69:45)
    at Object.<anonymous> (/Users/foo/.npm/_npx/6761/lib/node_modules/@commitlint/cli/node_modules/@commitlint/load/lib/index.js:142:17)

@escapedcat
Copy link
Member

@dexpota I just tried this on a clean Ubuntu VM:

npm install -g @commitlint/config-conventional @commitlint/cli
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > ~/.commitlintrc.js
echo "hello world" | commitlint

Output is:

⧗   input: hello world
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]
✖   found 2 problems, 0 warnings 
    (Need help? -> https://github.com/conventional-changelog/commitlint#what-is-commitlint )
cat ~/.commitlintrc.js
module.exports = {extends: ['@commitlint/config-conventional']}

I'm sorry, but I can't reproduce your issue.

This issue is related to #126 (comment)

@dexpota
Copy link
Author

dexpota commented Apr 12, 2019

Which version of Ubuntu are you using? I tried with Ubuntu 18.04 on a VM created with vagrant and I am having the same problem.

/usr/local/lib/node_modules/@commitlint/cli/lib/cli.js:113
        throw err;
        ^

Error: Cannot find module "@commitlint/config-conventional" from "/home/vagrant"
    at resolveId (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:112:14)
    at resolveConfig (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:92:20)
    at reduce (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:49:20)
    at Array.reduce (<anonymous>)
    at loadExtends (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:47:32)
    at resolveExtends (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:30:19)
    at Object.$If_1 (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/load/lib/index.js:69:45)
    at Object.<anonymous> (/usr/local/lib/node_modules/@commitlint/cli/node_modules/@commitlint/load/lib/index.js:142:17)
    at <anonymous>

Here more information about the tool versions:

vagrant@ubuntu-bionic:~$ npm --version                                                      
3.5.2
vagrant@ubuntu-bionic:~$ node --version
v8.10.0
vagrant@ubuntu-bionic:~$ git --version
git version 2.17.1
vagrant@ubuntu-bionic:~$ commitlint --version
7.5.2

and these are the steps to recreate the VM using vagrant:

vagrant init ubuntu/bionic64
vagrant up
vagran ssh

@escapedcat
Copy link
Member

Alright, it's the node version! I used Ubuntu 18.04 with latest npm/node. Sorry for saying clean. I should have mentioned that. Is it possible for you to update the node version?
I'm not sure how much effort we would put in to make this case work (again) in v8.

@dexpota
Copy link
Author

dexpota commented Apr 13, 2019

I tried with node version 10.15.3 and npm version 6.4.1 and now it's working.

@knksmith57
Copy link

knksmith57 commented Apr 13, 2019

to provide some color --it looks like our issue is slightly different from the one reported here.

I can deterministically reproduce the module resolution failure on both node 8.x and 10.x when using npx to fetch and run commitlint; however, I cannot reproduce on either 8.x or 10.x if the modules are first installed globally.

works with npm i -g
npm i -g @commitlint/config-conventional @commitlint/cli
echo 'hello world' \
  | commitlint -x @commitlint/config-conventional

❯ docker run --rm -i --entrypoint sh node:8.15.1 <<'EOT'
npm i -g @commitlint/config-conventional @commitlint/cli
echo 'hello world' \
  | commitlint -x @commitlint/config-conventional
EOT
/usr/local/bin/commitlint -> /usr/local/lib/node_modules/@commitlint/cli/lib/cli.js
+ @commitlint/config-conventional@7.5.0
+ @commitlint/cli@7.5.2
added 137 packages from 64 contributors in 9.439s

⧗   input: hello world
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]
✖   found 2 problems, 0 warnings
    (Need help? -> https://github.com/conventional-changelog/commitlint#what-is-commitlint )
❯ docker run --rm -i --entrypoint sh node:10.15.3 <<'EOT'
npm i -g @commitlint/config-conventional @commitlint/cli
echo 'hello world' \
  | commitlint -x @commitlint/config-conventional
EOT
/usr/local/bin/commitlint -> /usr/local/lib/node_modules/@commitlint/cli/lib/cli.js
+ @commitlint/config-conventional@7.5.0
+ @commitlint/cli@7.5.2
added 137 packages from 64 contributors in 10.263s

⧗   input: hello world
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]
✖   found 2 problems, 0 warnings
    (Need help? -> https://github.com/conventional-changelog/commitlint#what-is-commitlint )

fails with npx -p
echo 'hello world' \
  | npx \
    -p @commitlint/config-conventional \
    -p @commitlint/cli \
  commitlint -x @commitlint/config-conventional

❯ docker run --rm -i --entrypoint sh node:8.15.1 <<'EOT'
echo 'hello world' \
  | npx \
    -p @commitlint/config-conventional \
    -p @commitlint/cli \
  commitlint -x @commitlint/config-conventional
EOT
npx: installed 137 in 9.894s
/root/.npm/_npx/7/lib/node_modules/@commitlint/cli/lib/cli.js:113
	throw err;
	^

Error: Cannot find module "@commitlint/config-conventional" from "/"
    at resolveId (/root/.npm/_npx/7/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:112:14)
    at resolveConfig (/root/.npm/_npx/7/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:92:20)
    at reduce (/root/.npm/_npx/7/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:49:20)
    at Array.reduce (<anonymous>)
    at loadExtends (/root/.npm/_npx/7/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:47:32)
    at resolveExtends (/root/.npm/_npx/7/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:30:19)
    at Object.$If_1 (/root/.npm/_npx/7/lib/node_modules/@commitlint/cli/node_modules/@commitlint/load/lib/index.js:69:45)
    at Object.<anonymous> (/root/.npm/_npx/7/lib/node_modules/@commitlint/cli/node_modules/@commitlint/load/lib/index.js:142:17)
    at <anonymous>
❯ docker run --rm -i --entrypoint sh node:10.15.3 <<'EOT'
echo 'hello world' \
  | npx \
    -p @commitlint/config-conventional \
    -p @commitlint/cli \
  commitlint -x @commitlint/config-conventional
EOT
npx: installed 137 in 8.683s
/root/.npm/_npx/9/lib/node_modules/@commitlint/cli/lib/cli.js:113
	throw err;
	^

Error: Cannot find module "@commitlint/config-conventional" from "/"
    at resolveId (/root/.npm/_npx/9/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:112:14)
    at resolveConfig (/root/.npm/_npx/9/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:92:20)
    at reduce (/root/.npm/_npx/9/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:49:20)
    at Array.reduce (<anonymous>)
    at loadExtends (/root/.npm/_npx/9/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:47:32)
    at resolveExtends (/root/.npm/_npx/9/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/lib/index.js:30:19)
    at Object.$If_1 (/root/.npm/_npx/9/lib/node_modules/@commitlint/cli/node_modules/@commitlint/load/lib/index.js:69:45)
    at Object.<anonymous> (/root/.npm/_npx/9/lib/node_modules/@commitlint/cli/node_modules/@commitlint/load/lib/index.js:142:17)

At this point I'm thinking this is either an issue with @commitlint/resolve-extends or a bug in npx.

@byCedric byCedric added the bug label Apr 26, 2019
@byCedric
Copy link
Member

I did some digging too because I really really want to implement zero-config and the GitHub Action 😅 There is actually a chain of dependencies, working together making global imports work, here it goes:

  1. @commitlint/cli - requests resolved configuration from @commitlint/resolve-extends
  2. @commitlint/resolve-extends - fetches configuration based on module ids with resolve-from and/or resolve-global
  3. resolve-global - needs a place to look at for global modules, so it requests a path to this with global-dirs
  4. global-dirs - currently has an issue where it resolves the symlink origin of the installed Node, instead of resolving to the proper global folder.

There we go, that's our issue too. I tried adding a 3rd fallback in @commitlint/resolve-extends to look into the hardcoded /usr/local/lib/node_modules folder, and it worked.

I'm not sure if we should drop or swap out resolve-global, because it's a nice library and has support for both Yarn and NPM. Maybe we can pitch in with Sindre to get this fixed? There are some great developers who have documented a lot in that issue, so it's up for grabs to fix it I guess.

For now, I can confidently add the "bug" label to this until that issue is resolved.

@byCedric
Copy link
Member

byCedric commented Apr 30, 2019

Little status update: a PR to fix the underlying issue as described above is merged. Now we have to wait for a release and then we can test if it's still broken or not. 😄

To quote Sindre (maker of the package):

I'll do a new release once the other open PRs are merged.

@byCedric
Copy link
Member

byCedric commented May 2, 2019

New 1.0.0 release of the global-dirs is released! I'll work on this issue tonight (CEST), will check if the 1.0.0 is included in our semver range and test if it still persists after that.

@byCedric
Copy link
Member

byCedric commented May 2, 2019

I've tested this one, and unfortunately global-dirs@1.0.0 isn't here (yet). Right now, resolve-global needs to be updated. Luckily, I created a PR for this, let's hope it's merged (& released) soon! 😄

@byCedric
Copy link
Member

byCedric commented May 6, 2019

Quick status update, Yarn still has some issues with global-dirs. Stay tuned for more updates 😄

@nfantone
Copy link

nfantone commented Oct 1, 2019

@byCedric Any new updates on this? Still tuned here.

@byCedric
Copy link
Member

byCedric commented Oct 1, 2019

Unfortunately, it's still blocked by sindresorhus/global-directory#13. 😞 Apparently, its kind of a hard issue to solve. If it's not solvable in that library, we might have to rethink if we can solve this issue without this library.

But that's a decision where also @marionebl and @escapedcat must be involved.

@fastman
Copy link

fastman commented Nov 18, 2019

Yup, it would be great to use it like @knksmith57 mentioned in the second example.
So having only commitlint.config.js file with

module.exports = {extends: ['@commitlint/config-conventional']}

and running:

npx -p @commitlint/config-conventional -p @commitlint/cli -c 'echo "test" |commitlint'

So the project can either have it own rules/config (if it's included in package.json) or use default by only including the basic config file (eg. for non-nodejs projects)

@Kcaby
Copy link

Kcaby commented Mar 18, 2020

hey brother, When can this problem be solved.
Are there other solutions
thx!!!

@escapedcat
Copy link
Member

@Linyutau there's no timeline. This is more or less blocked by sindresorhus/global-directory#13

@Lagily
Copy link

Lagily commented Mar 27, 2020

A bit unrelated, but in case it helps somebody: This error also occurs without having commitlint installed globally: if you have your commitlint.config.js a level above where your package.json is, the config is still found, but the OPs error occurs. For me, it was a simple mistake and when I copied the config to the same level as the package.json, it worked fine again.

@mseele
Copy link

mseele commented Jun 9, 2020

i run into the same bug under macOS. Is there any workaround to get it work as global installation?

@paesrafael
Copy link

Guys, here the same problem happened when installing globally.

I solved it by adding the full path to the @commitlint/config-conventional folder in my commitlint.config.js in each project.

Example:
module-yarn-global-commitlint

This is far from the best way, but it resolved.

@OmgImAlexis
Copy link
Contributor

@paesrafael guessing you don’t work with any others as that’s only gonna work for your computer.

@CrispyDrone
Copy link

I was experiencing the same issue see #847.

However, I recently upgraded to node 10, and will try to retest this in the next couple of days since it seems that fixed the issue for some people in this thread.

@nikoladsp
Copy link

@paesrafael guessing you don’t work with any others as that’s only gonna work for your computer.

One year later, I am experiencing same issue with node v14.17.3 and npm 6.14.13 and globally installed commitlint. His reply actually helped me to focus on actual work

@mamachanko
Copy link

Can confirm that it does not work.

fwiw semantic-release’s plugins work as expected with npx. Knowing nothing about the implementation at least it suggests that this isn't a fundamental problem.

npx \
  --yes \
  --package semantic-release@18.0.0 \
  --package @semantic-release/exec@6.0.2 \
  -- \
    semantic-release

@dbrab
Copy link

dbrab commented Feb 26, 2022

For some reason, I had created the config file outside my working directory, one level back, then commitlint when trying to find the module, it didn't find it because node_modules was one level inside, I changed the config inside and it worked fine.

@djantea
Copy link

djantea commented Mar 8, 2022

With @commitlint/cli and @commitlint/config-conventional installed globally, on Linux, I got it working like this:

echo "hello world" | commitlint -g ~/.commitlintrc.js -x $(npm root -g)/@commitlint/config-conventional

abdes added a commit to asap-projects/asap-common that referenced this issue Mar 8, 2022
commitlint has some issue working with globally installed npm packages.
See conventional-changelog/commitlint#613 for
more details.

The workaround is to not set the commitlint config in its config file
but instead pass the full path to the module in the command line of
commitlint.
abdes added a commit to abdes/asap that referenced this issue Mar 8, 2022
commitlint has some issue working with globally installed npm packages.
See conventional-changelog/commitlint#613 for
more details.

The workaround is to not set the commitlint config in its config file
but instead pass the full path to the module in the command line of
commitlint.
@politician
Copy link

politician commented Jun 21, 2022

I am also having the same problem. I would like to use commitlint on a non-node.js project (with no package.json).

Reproduce

cat <<EOF > .commitlintrc.yaml
extends:
  - '@commitlint/config-conventional'
EOF

echo "test" | npm exec --yes\
  --package @commitlint/config-conventional\
  --package @commitlint/cli\
  commitlint

Error:

Error: Cannot find module "@commitlint/config-conventional" from "/Users/romain/Code/politician/template-repo"
    at resolveId (/Users/romain/.npm/_npx/bda3302a37d8221c/node_modules/@commitlint/resolve-extends/src/index.ts:131:14)
    at resolveConfig (/Users/romain/.npm/_npx/bda3302a37d8221c/node_modules/@commitlint/resolve-extends/src/index.ts:105:20)
    at /Users/romain/.npm/_npx/bda3302a37d8221c/node_modules/@commitlint/resolve-extends/src/index.ts:51:20
    at Array.reduce (<anonymous>)
    at loadExtends (/Users/romain/.npm/_npx/bda3302a37d8221c/node_modules/@commitlint/resolve-extends/src/index.ts:49:13)
    at resolveExtends (/Users/romain/.npm/_npx/bda3302a37d8221c/node_modules/@commitlint/resolve-extends/src/index.ts:25:19)
    at load (/Users/romain/.npm/_npx/bda3302a37d8221c/node_modules/@commitlint/load/src/load.ts:56:33)
    at async main (/Users/romain/.npm/_npx/bda3302a37d8221c/node_modules/@commitlint/cli/src/cli.ts:202:17) {
  code: 'MODULE_NOT_FOUND'
}

@mamachanko mentioned semantic-release's plugin system works well. I would like to add that textlint's plugin system also works:

npm exec --yes\
  --package textlint-filter-rule-allowlist\
  --package textlint-filter-rule-comments\
  --package textlint-rule-terminology\
  textlint .

@politician
Copy link

politician commented Jun 21, 2022

Workaround for those in the same case as me, manually include the rules from your chosen config in your commitlint.config.js config file.

Here are the rules for @commitlint/config-conventional in YAML:

.commitlintrc.yaml

---
# The rules below have been manually copied from @commitlint/config-conventional
# and match the v1.0.0 specification:
# https://www.conventionalcommits.org/en/v1.0.0/#specification
#
# You can remove them and uncomment the config below when the following issue is
# fixed: https://github.com/conventional-changelog/commitlint/issues/613
#
# extends:
#   - '@commitlint/config-conventional'
rules:
  body-leading-blank: [1, always]
  body-max-line-length: [2, always, 100]
  footer-leading-blank: [1, always]
  footer-max-line-length: [2, always, 100]
  header-max-length: [2, always, 100]
  subject-case:
    - 2
    - never
    - [sentence-case, start-case, pascal-case, upper-case]
  subject-empty: [2, never]
  subject-full-stop: [2, never, "."]
  type-case: [2, always, lower-case]
  type-empty: [2, never]
  type-enum:
    - 2
    - always
    - [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]

beatrizsmerino added a commit to beatrizsmerino/vue-todolist that referenced this issue Aug 16, 2022
- For the husky v.8 not use the `.huskyrc` file for set the configuration.
Set the configuration inside the `.husky` folder, in the individual files as the `commit-msg` hook.
https://typicode.github.io/husky/#/?id=migrate-from-v4-to-v8

Steps:
- Add the next command to the `commit-msg` hook:
`npx --no -- commitlint --edit $1`

Error:
- Error: Cannot find module "@commitlint/config-conventional" from "/Applications/MAMP/htdocs/vue-todolist" at resolveId (/Users/beatrizsmerino/.npm/_npx/16d437951fea4b5e/node_modules/@commitlint/resolve-extends/src/index.ts:131:14)
- ✖   body's lines must not be longer than 100 characters [body-max-line-length]
✖   found 1 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

Solution:
- Install locally the packages:
`npm install @commitlint/cli @commitlint/config-conventional --save-dev`
`npm install commitizen cz-conventional-changelog --save-dev`
- Update the configuration `commitlint`, increase the `body-max-line-length`

References:
- https://www.youtube.com/watch?v=jNxDNoYEGVU
- https://www.youtube.com/watch?v=OJqUWvmf4gg
- https://gist.github.com/qoomon/5dfcdf8eec66a051ecd85625518cfd13
- https://github.com/qoomon/git-conventional-commits
- https://remarkablemark.org/blog/2019/05/29/git-husky-commitlint/
- https://typicode.github.io/husky/#/?id=husky_git_params-ie-commitlint-
- https://stackoverflow.com/a/67074398
- https://rahulgurung.com/How-to-use-the-latest-Husky-8-with-Commitizen-for-adding-git-hooks-to-your-projects/
- https://www.techiediaries.com/git-hooks-husky-commitlint/
- https://www.freecodecamp.org/news/how-to-use-commitlint-to-write-good-commit-messages/
- https://medium.com/dottech/mejorando-los-mensajes-de-git-commit-con-husky-y-commitlint-7bddf6ab22c2
- commitizen/cz-cli#289
- https://github.com/conventional-changelog/commitlint
- conventional-changelog/commitlint#613
- https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/config-conventional/index.js
- https://github.com/conventional-changelog/commitlint/blob/master/docs/reference-rules.md#body-max-line-length
@qRoC
Copy link

qRoC commented Nov 3, 2022

Just set NODE_PATH, in my case:

export NODE_PATH="$XDG_DATA_HOME/yarn/global/node_modules:$XDG_DATA_HOME/npm/lib/node_modules"

@chunleng
Copy link

Just set NODE_PATH, in my case:

export NODE_PATH="$XDG_DATA_HOME/yarn/global/node_modules:$XDG_DATA_HOME/npm/lib/node_modules"

I have it working on one out of 2 terminals and this gave me a hint of what happened.

Using npm config list it seems like the one that is not working has this additional line:

; "env" config from environment

prefix = "~/.asdf/installs/nodejs/lts-gallium/.npm"

which install all my package in a slightly different directory that is never going to be sourced. Because on my environment the correct directory to install into should be 1 level up and not in the .npm folder

@virtuallyunknown
Copy link

virtuallyunknown commented Jun 1, 2023

This might be a bit late, but here is my setup:

  1. Install @commitlint/cli and @commitlint/config-conventional globally via npm.
sudo npm i -g @commitlint/cli @commitlint/config-conventional
  1. Create the following alias for commitlint in your shell configuration file, like .zsh-aliases for example.
alias commitlint='commitlint --extends "@commitlint/config-conventional"'
  1. Success.

Now you can use commitlint with config-conventional from anywhere, without having to create a .commitlintrc.js in every project.

> echo 'hello world' | commitlint 
⧗   input: hello world
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

> echo 'feat: use commitlint globally' | commitlint -c false
# no output, means it works

> commitlint --print-config
{
  extends: [ '@commitlint/config-conventional' ],
  formatter: '@commitlint/format',
  .....
}

@tombh
Copy link

tombh commented Jul 1, 2023

Just to add to @politician's great comment, the command then becomes something like: npx commitlint --from main --to HEAD --verbose --config commitlintrc.yaml.

I have to say that I'm somewhat shocked that this was so difficult to find. Does nobody outside of the JS ecosystem use commitlint?

@adamliter
Copy link

I was also facing this issue when using node installed by Homebrew on a Mac (which gets installed to /opt/homebrew on Macs with the newer ARM-based chips). The suggestion of setting NODE_PATH worked for me. I just added export NODE_PATH=$(npm root -g) to my .profile file.

adamliter added a commit to adamliter/conf that referenced this issue Sep 9, 2023
Note that in order to use local .commitlinrc.yml files which extend the
`@commitlint/config-conventional` settings, we need to explicitly set
NODE_PATH. Without doing this, `commitlint` will give an error saying it
cannot find the module. See
conventional-changelog/commitlint#613 for more
discussion.
@Clockwork-Muse
Copy link

I'm trying to do this as well, but from inside the husky commit-msg hook, and despite attempting some of the workarounds I'm still getting various errors.

From inside a devcontainer, globally installing the packages and init-ing husky:

npm install --yes -g @commitlint/cli @commitlint/config-conventional
npm install --yes -g husky && npx husky install

The husky commit-msg hook is as so:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

commitlint --extends "@commitlint/config-conventional" --edit ${1}

... and yet it doesn't find the module:

> > git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
Error: Cannot find module "@commitlint/config-conventional" from "/workspaces/item-api"
    at resolveId (/usr/local/share/nvm/versions/node/v18.18.0/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/src/index.ts:131:14)
    at resolveConfig (/usr/local/share/nvm/versions/node/v18.18.0/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/src/index.ts:105:20)
    at /usr/local/share/nvm/versions/node/v18.18.0/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/src/index.ts:51:20
    at Array.reduce (<anonymous>)
    at loadExtends (/usr/local/share/nvm/versions/node/v18.18.0/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/src/index.ts:49:13)
    at resolveExtends (/usr/local/share/nvm/versions/node/v18.18.0/lib/node_modules/@commitlint/cli/node_modules/@commitlint/resolve-extends/src/index.ts:25:19)
    at load (/usr/local/share/nvm/versions/node/v18.18.0/lib/node_modules/@commitlint/cli/node_modules/@commitlint/load/src/load.ts:56:33)
    at async main (/usr/local/share/nvm/versions/node/v18.18.0/lib/node_modules/@commitlint/cli/src/cli.ts:214:17) {
  code: 'MODULE_NOT_FOUND'
}
husky - commit-msg hook exited with code 1 (error)

It's not clear why; calling the command in a normal terminal succeeds (eg, echo "hello" | commitlint --extends "@commitlint/config-conventional" fails as expected).

I can get it to succeed by writing a config file, but I have to comment out/remove parserPreset.
I can also get it to succeed by saving off NODE_PATH in the devcontainer config, but would require updating for patch versions of node, which is likely to be deployed automatically.

npm config list prints out:

; node bin location = /usr/local/share/nvm/versions/node/v18.18.0/bin/node
; node version = v18.18.0
; npm local prefix = /workspaces/item-api
; npm version = 9.8.1
; cwd = /workspaces/item-api
; HOME = /home/vscode
; Run `npm config ls -l` to show all defaults.

from both the husky hook and the terminal window.

@ehrktia
Copy link

ehrktia commented Oct 15, 2023

I got it to work by installing packages not-exactly-globally.
You install npm packages to the user's home directory, e.g. by following these instructions.
I have seen it work with nvm, but apparently that does not work for everyone (see @OmgImAlexis' comment below).

Thanks, that helped me!

anyone using osx facing a similar issue with error MODULE_NOT_FOUND ,
a quick option is to get npm set in user's home directory.
set up npm to install packages in the custom directory
Thanks very much

@Realiserad
Copy link
Contributor

Realiserad commented Dec 4, 2023

I had the same issue. Solved it by running commitlint inside a container and added an alias to my shell (fish) as follows:

alias commitlint="docker run --user node -v ./:/home/node:ro -i commitlint/commitlint --extends '@commitlint/config-conventional' --cwd /home/node"

@carhartl
Copy link

Here's how I managed to arrive at a zero config solution (workaround):

npx --yes --package="@commitlint/cli" --package="@commitlint/config-conventional" -c "command -v commitlint | sed 's/.bin\/commitlint$//' > prefix.temp; git exclude prefix.temp; commitlint --extends \"$(cat prefix.temp)@commitlint/config-conventional\" --edit"

Requires an intermediary temp file, which I didn't manage to get rid of..

@virtuallyunknown
Copy link

A little update from my previous comment in this thread, I decided to migrate from NPM to PNPM recently, and things fell apart.

Currently I have both @commitlint/cli and @commitlint/config-conventional globally with pnpm, but when I run this command it would throw an error since it can no longer locate config-conventional.

commitlint --extends "@commitlint/config-conventional" --print-config

/home/user/.local/share/pnpm/global/5/.pnpm/@commitlint+cli@18.6.1_@types+node@20.11.17_typescript@5.3.3/node_modules/@commitlint/cli/lib/cli.js:123
        throw err;
        ^

Error: Cannot find module "@commitlint/config-conventional" from "/home/user/projects/example"
    at resolveId (/home/user/.local/share/pnpm/global/5/.pnpm/@commitlint+resolve-extends@18.6.1/node_modules/@commitlint/resolve-extends/lib/index.js:104:17)
    at resolveConfig (/home/user/.local/share/pnpm/global/5/.pnpm/@commitlint+resolve-extends@18.6.1/node_modules/@commitlint/resolve-extends/lib/index.js:88:26)
    at /home/user/.local/share/pnpm/global/5/.pnpm/@commitlint+resolve-extends@18.6.1/node_modules/@commitlint/resolve-extends/lib/index.js:47:26
    at Array.reduce (<anonymous>)
    at loadExtends (/home/user/.local/share/pnpm/global/5/.pnpm/@commitlint+resolve-extends@18.6.1/node_modules/@commitlint/resolve-extends/lib/index.js:45:16)
    at resolveExtends (/home/user/.local/share/pnpm/global/5/.pnpm/@commitlint+resolve-extends@18.6.1/node_modules/@commitlint/resolve-extends/lib/index.js:25:22)
    at load (/home/user/.local/share/pnpm/global/5/.pnpm/@commitlint+load@18.6.1_@types+node@20.11.17_typescript@5.3.3/node_modules/@commitlint/load/lib/load.js:42:52)
    at async main (/home/user/.local/share/pnpm/global/5/.pnpm/@commitlint+cli@18.6.1_@types+node@20.11.17_typescript@5.3.3/node_modules/@commitlint/cli/lib/cli.js:162:24) {
  code: 'MODULE_NOT_FOUND'
}

Node.js v20.11.0

This is a speculation on my part, since I haven't studied the source code, but I think @commitlint+resolve-extends only knows how to handle global packages installed via NPM (and yarn?), but not PNPM.

Either way, I ended up with yet another workaround. In .zsh-aliases, change the previous alias to:

alias commitlint='commitlint --extends "$(pnpm root -g)/@commitlint/config-conventional/lib/index.js"'

As a closing thought I would say this - I am grateful to all devs and maintainers for providing us these wonderful tools for free, but I wish global configuration was made easier, since I believe a lot of folks out there use the cli globally.

@escapedcat
Copy link
Member

No sure if pnpm ever worked with this or not but I know that there is at least one PR related to pnpm. Would be great to have someone who could take care of checking and testing pnpm support.

@JounQin
Copy link
Contributor

JounQin commented Feb 28, 2024

Can you try latest v19?

@carhartl
Copy link

Not sure if the updates were targeting this scenario, but I still can't get this to work:

npx --yes --package="@commitlint/cli" --package="@well-crafted/commitlint-config" \
  commitlint --extends "@well-crafted/commitlint-config" --edit

@JounQin
Copy link
Contributor

JounQin commented Mar 8, 2024

@carhartl I don't think we support such usage.

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

No branches or pull requests