commitizen / cz-cli

The commitizen command line utility. #BlackLivesMatter
http://commitizen.github.io/cz-cli/
MIT License
16.7k stars 547 forks source link

commitizen init fails when commitizen is installed with -g #418

Open jimthedev opened 7 years ago

jimthedev commented 7 years ago

Installing commitizen globally using the steps in the documentation as shown here

npm install -g commitizen
npm install -g cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

Prevents you from being able to run commitizen init --save-dev cz-conventional-changelog in a brand new npm project.

Attempting to initialize using the npm package cz-conventional-changelog
Error: A previous adapter is already configured. Use --force to override

Actually running commitizen init --save-exact --save-dev cz-conventional-changelog --force results in:

Attempting to initialize using the npm package cz-conventional-changelog
npm WARN using --force I sure hope you know what you are doing.
npm WARN saveError ENOENT: no such file or directory, open '/Users/jim.cummins/package.json'
/Users/jim.cummins
└─┬ cz-conventional-changelog@1.2.0
  ├── conventional-commit-types@2.1.0
  ├── lodash.map@4.6.0
  ├── longest@1.0.1
  ├─┬ pad-right@0.2.2
  │ └── repeat-string@1.6.1
  ├── right-pad@1.0.1
  └── word-wrap@1.2.0

npm WARN enoent ENOENT: no such file or directory, open '/Users/jim.cummins/package.json'
npm WARN jim.cummins No description
npm WARN jim.cummins No repository field.
npm WARN jim.cummins No README data
npm WARN jim.cummins No license field.
{ Error: ENOENT: no such file or directory, open '/Users/jim.cummins/package.json'
    at Error (native)
    at Object.fs.openSync (fs.js:640:18)
    at Object.fs.readFileSync (fs.js:508:33)
    at addPathToAdapterConfig (/Users/jim.cummins/.nvm/versions/node/v6.9.1/lib/node_modules/commitizen/dist/commitizen/adapter.js:71:40)
    at init (/Users/jim.cummins/.nvm/versions/node/v6.9.1/lib/node_modules/commitizen/dist/commitizen/init.js:104:9)
    at Object.bootstrap (/Users/jim.cummins/.nvm/versions/node/v6.9.1/lib/node_modules/commitizen/dist/cli/commitizen.js:43:30)
    at Object.<anonymous> (/Users/jim.cummins/.nvm/versions/node/v6.9.1/lib/node_modules/commitizen/bin/commitizen.js:2:38)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/Users/jim.cummins/package.json' }

I think this is because when running init we need to assume that the user wants to only init in the current directory, as such we should not be using findup to get the highest npm path like we do when looking for an adapter. Instead we need to be stricter in the case of init. We should explicitly be looking in the current directory for a package.json or we should fail.

ericmarkmartin commented 5 years ago

I just ran into this problem and it seems that adding the --force flag worked for me, but I did already have a package.json.

kgbook commented 5 years ago

Enter the directory of the project, and run npm init --yes to generate package.json, then execute commitizen init cz-conventional-changelog --save --save-exact.

It works for me.

rohangore1999 commented 1 year ago

Any update on above issue?