cz-cli icon indicating copy to clipboard operation
cz-cli copied to clipboard

commitizen init fails when commitizen is installed with -g

Open jimthedev opened this issue 9 years ago • 3 comments

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
└─┬ [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ └── [email protected]
  ├── [email protected]
  └── [email protected]

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.

jimthedev avatar Jan 09 '17 21:01 jimthedev

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.

ericmarkmartin avatar Jan 14 '19 00:01 ericmarkmartin

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.

kgbook avatar Apr 10 '19 15:04 kgbook

Any update on above issue?

rohangore1999 avatar Dec 30 '22 18:12 rohangore1999