Could not find a package.json file. Run 'npm init' to create one. Error: Could not find a package.json file. Run 'npm init' to create one. at check (/Users/sdhaksh5/.nvm/versions/node/v10.16.0/lib/node_modules/eslint/lib/init/npm-utils.js:106:15) at Object.checkDevDeps (/Users/sdhaksh5/.nvm/versions/node/v10.16.0/lib/node_modules/eslint/lib/init/npm-utils.js:160:12) at getModulesList (/Users/sdhaksh5/.nvm/versions/node/v10.16.0/lib/node_modules/eslint/lib/init/config-initializer.js:126:40) at inquirer.prompt.then.earlyAnswers (/Users/sdhaksh5/.nvm/versions/node/v10.16.0/lib/node_modules/eslint/lib/init/config-initializer.js:547:29) at process._tickCallback (internal/process/next_tick.js:68:7)

When running eslint in a folder with javascript files, that is not based on any package management modules, the eslint --init command fails with the above error. When running the command, it asks for a set of questions to configure the init process that involves

  • Run the command in the browser / node.
  • Usage of any package management modules. (npm )
  • React of Vue.js project

The choice we make for the above questions pretty much determine how the init process runs. I answered such that i don't use any package management modules, because my project is plain vannila javascript (a chrome extension actually). But still i kept on getting the same error. On digging into the code following the stack trace it looks like, the --init process tries to install auxilary eslint libraries that are required to run the cli. Remember that there are two parts to eslint.

  1. eslint-cli
  2. eslint

The cli is what you use to run eslint. but any command that you invoke from it requires the eslint libraries. So i installed the eslint libraries using npm install -g eslint, so that it's available to run it from any packages. And after this create an empty package.json in the current folder and add the following

{
}

This is to make sure that we don't have any packages. Now run eslint --init, it should run without issues.

I originally did this to make eslint work with vim so that i can develop my chrome extension that will be used to fix a missing feature in Bamboo to download artifacts from a completed build, which is inturn used to run the artifacts (.NET Fx) with fortify in Mac because you cannot build the .NET Fx binaries in Mac, which is inturn done because of fortify installation fails in windows 10 (x64) and a fix to this is void by our security team :-(

Never give up because your security team doesn't give a what !