Introduction to npm, npx, NodeJS and React

npm is an open-source software package manager originally developed for JavaScript packages to be run with Node.js, but also used for other packages. It is an interface to the npm repository. It allows JavaScript developers to share packages quickly and easily.

npx is the node package runner

How to see which version you have:

$ npm -v
$ node -v

How to get the latest Node.js version using the n package:

$ sudo npm install -g n
$ sudo n stable

  installing : node-v14.15.1
       mkdir : /usr/local/n/versions/node/14.15.1
       fetch :
   installed : v14.15.1 (with npm 6.14.8)

Sonia M wrote a blog post What Is NPM (Node Package Manager)?

It is an npm package, but it isn’t

The React front end for Lino repository is an npm package (because it has an package.json ), but we don’t publish it on npm. That’s not needed because everything needed to run a Lino site with the React front end is made available as Django static files inside the lino-react Python package. The whole source code is published on github. You need npm and node only for developing Lino React, not for using it.


Creating your own packages

npm packages must contain at least a file named package.json, which must exist in their top-level directory. This file can be generated by npm init and later edited manually. But there is a tool that creates a whole package with a working react app, including a package.json:

$ npx create-react-app myapp

Lino React files reference


The npm package description for lino_react.

The package.json for lino_react mainly defines a few “scripts” (i.e. commands):

"scripts": {
  "debug": "webpack --mode none ",
  "dev": "webpack --mode development",
  "build": "webpack --mode production",
  "build_css": "node_modules/node-sass-chokidar/bin/node-sass-chokidar ./lino_react/react/components/layout -o ./lino_react/react/components/layout",

The commands debug, dev and build create the main.js file. They are very similar, their only difference is the –mode option, which tells webpack to use its built-in optimizations accordingly.

webpack is a tool that bundles all the “assets” into a set of deployable files (.js, .css etc). We use it to compile the index.js file into the main.js file.

Our build_css script compiles scss files to css files. We need to run it only after changing one of our scss files which are located in lino_react/react/components/layout.

build_css runs node-sass-chokidar, which is a “thin wrapper around node-sass executable to use chokidar instead of Gaze when watching files.” Node-sass is “a library that provides binding for Node.js to LibSass, the C version of the popular stylesheet preprocessor, Sass. It allows you to natively compile .scss files to css at incredible speed and automatically via a connect middleware.”


Our configuration file for webpack. TODO: what does it do?


Contains a single line of code:

import App from "./components/App";

This file is in lino_react/react/static/react/ where the collectstatic on a Lino site will find it.

It contains a lot of generated and compressed JS code.

It is quite big and therefore causes webpack to issue a warning:

WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
This can impact web performance.
  main.js (1.24 MiB)