Npm install fails due to package.json misconfiguration

AI-generated summary

The issue of npm install failing due to a package.json misconfiguration has been discussed. After a recent commit, npm install fails for dependencies with versions referencing Git repositories instead of semantic version strings, particularly if no package.json file exists in the repository.

kleinfreund reported the issue, and pmusaraj explained that npm install is not supported for development, and yarn install should be used instead. The error is due to the use of Yarn syntax for referencing GitHub versions of dependencies in package.json.

It was suggested to add an entry to package.json to force npm install to fail with a message indicating that only Yarn is supported sam. A possible solution was found joffreyjaffeux, but it was later determined that Yarn and NPM use incompatible syntax for referencing remote Git repositories.

A commit has been made to add a preinstall hook that stops npm install and outputs a message to use Yarn instead pmusaraj. Users are advised to use yarn install for development purposes.

Since the commit Use Yarn to manage dependencies, cleanup unused bootstrap libs (#6436) · discourse/discourse@dc8a16b · GitHub, running npm install fails.

Looking at the log file …

880 silly fetchPackageMetaData error for jquery.autoellipsis@git+https://github.com/pvdspek/jquery.autoellipsis.git premature close
881 silly fetchPackageMetaData error for favcount@git+https://github.com/chrishunt/favcount.git premature close
882 silly fetchPackageMetaData error for jquery-resize@git+https://github.com/cowboy/jquery-resize.git premature close

… installation fails for dependencies whose version is a git repository instead of a semantic version string. In particular, they fail if in the repostitory, there is no package.json file.

Discourse’s dependencies from package.json:

"dependencies": {
  "ace-builds": "1.4.1",
  "bootbox": "3.2.0",
  "chart.js": "2.7.2",
  "favcount": "https://github.com/chrishunt/favcount",
  "handlebars": "4.0.5",
  "htmlparser": "https://github.com/tautologistics/node-htmlparser",
  "jquery": "3.3.1",
  "jquery-color": "1.0.0",
  "jquery-resize": "https://github.com/cowboy/jquery-resize/",
  "jquery.autoellipsis": "https://github.com/pvdspek/jquery.autoellipsis",
  "jquery.cookie": "1.4.1",
  "jquery-tags-input": "1.3.5",
  "magnific-popup": "1.1.0",
  "markdown-it": "8.4.1",
  "mousetrap": "https://github.com/discourse/mousetrap#firefox-alt-key",
  "pikaday": "1.7.0",
  "resumablejs": "1.1.0",
  "spectrum-colorpicker": "1.8.0"
}

Is npm install still supposed to work or is Yarn the only option here?

This is a development question, not an install question, yes?

Is it? I wasn’t quite sure. Are these dependencies only relevant for Discourse’s testing? Then the category was wrong indeed. Sorry about that.

@pmusaraj any tips here? Also … why are you using npm, we only really test using yarn and only support yarn for dev.

I think the error you’re getting is due to our package.json using yarn syntax for referencing Github versions of dependencies. So, use yarn install instead of npm. Also, yarn install is only required for running frontend (qunit) tests or when a developer is upgrading one of the Discourse dependencies listed in package.json.

I wonder if there is some entry we can add to our package.json that would force npm install to explode with a “sorry, npm is not supported use yarn”

Ultra low priority though.

@pmusaraj that seems like a good track to follow:

I did not know that only Yarn is supposed to be used here. I saw a package.json and run npm install a short while ago to run my plugin tests. Now, npm install doesn’t work.

It looks like Yarn and NPM use incompatible syntax for referencing remote git repositories via URL so there is no nice common ground.
Yarn does seem to support this as per git+ssh install protocol is not used correctly · Issue #2500 · yarnpkg/yarn · GitHub

However, the missing package.json in these repositories makes npm fail.

@kleinfreund there is absolutely no need to run npm install. And as Sam and Joffrey point out, we will shortly block running npm install on the repo. (Just use yarn install.)

I get it. It’s just that I didn’t previously need Yarn, so I have to install it. That’s not a big deal though.

This commit adds a preinstall hook that stops npm install and outputs “NPM is not supported, please use Yarn instead.” to the user.
https://github.com/discourse/discourse/commit/0fdb4d0e46e8a9f0bfae9368546c99b8030b5d48