Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

NPM

From MoodleDocs
Revision as of 12:31, 17 December 2019 by Eloy Lafuente (stronk7) (talk | contribs) (add missing npm shrinkwrap execution)

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.

NPM is a package manager for JavaScript. It is used by Moodle developers to install a number of cli tools, primarily those related with Grunt.

npm dependency management

Tools installed by npm include things like our css/js compressors, post-processors and integrity checks. As differences in output from these tools would cause different results for different developers working on the same code, we need to ensure installed packages are the same.

npm shrinkwrap

We use a npm-shrinkwrap.json to ensure that all versions of packages are consistent for developers when they run `npm install`. See https://docs.npmjs.com/files/package-locks for further details on this mechanism. Note: The npm package lock mechanics changed significantly in NPM version 5. Also note that some changes in node itself can cause changes to the npm-shrinkwrap.json file, for this reason it is recommend that you use the latest lts version of node when running `npm install`. See below for more information about versions.

Updating npm dependencies

Periodically we will update our npm dependencies, the process for doing this is:

  1. Ensure that you have the correct version of NPM/Node installed...
    1. If you are using NVM, then you can (since Moodle 3.5) just run nvm use
    2. If you are not... go and install NVM and the run step 1.1 (or install the current version, >=16.14.0 <17 (a.k.a. "lts/gallium"). Right now pointing to nodejs v16.15.1 (see MDL-73915 for more details).).
  2. Perform the required changes to the package.json file. Normally.
  3. Remove the npm-shrinkwrap.json file that is going to be regenerated. rm npm-shrinkwrap.json
  4. Remove completely the node_modules directory. rm -fr node_modules
  5. Clean all npm caches. npm cache clear --force
  6. Run npm install, all packages will be installed.
  7. Run npm shrinkwrap, a new npm-shrinkwrap.json will be regenerated
  8. Check that the npm-shrinkwrap.json changes match the modifications performed in the package.json file.
  9. Verify that grunt, behat tests... do continue working ok.
  10. Done, you can send the changes for review, integration and, if everything goes ok, will be applied upstream without problem.

NPM versions

In general, you will need to use the latest LTS version available, more exactly >=16.14.0 <17 (a.k.a. "lts/gallium"). Right now pointing to nodejs v16.15.1 (see MDL-73915 for more details).

Switching NPM versions

We have discovered upstream npm bugs (MDL-59103, MDL-63346) when upgrading NPM version with a previously installed node_modules directory. It is recommended to clear the node_modules directory and re-running `npm install` when changing npm version to avoid these problems. Sometimes it is required to clear the node modules cache as well (do this if you see errors about mis-matching hashes). To clear the node modules cache run "npm cache clear --force".