Skip to content

introduction

Simply : javascript from the command line. Rhino does that as well but... But like it is in the browser, for instance console.log() just works and also you get more modules installable through npm the nodejs package manager.

installation

Node and npm have to be setup correctly. A default install seems to lead to npm -g installing everything in /usr/lib/node_modules and we need root permissions there.

never do sudo npm -g install ...

Since anyone can virtually post a package on the npm repositories, we could end up with a malicious script with root permissions.

If you are already seeing npm -g using /usr/lib, try reinstalling it according to this link : visit

Install from tar file or with nvm

This is for newer versions !!. On debian apt-get install nodejs gives you V12 and that seems to do fine !!

The first one is better because with nvm you seem to have to type this every single time you start a new shell.

use node
nvm use node

So you will probably do the tar install anyway to get rid of it. The tar install is simply unpacking and adding that location to your path.

tar file

browse to visit to pick a version you want. or..

install
1
2
3
4
cd ~/Install
wget https://nodejs.org/dist/v16.13.1/node-v16.13.1-linux-x64.tar.xz
tar xv node-v16.13.1-linux-x64.tar.xz
vi ~/bin/kees.sh # add NODE_HOME/bin to PATH

nvm

Here are the steps from there in terse mode

tldr
apt-get remove nodejs
# look at https://github.com/nvm-sh/nvm if next is the latest version :
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash
# it will alter your .bashrc so open a new xterm or bash shell 
bash
nvm ls-remote # lists all node versions
nvm install node
node --version  # v13.3.0
nvm list # some install info
npm -v   # 6.13.1

If new shells fail to find npm it might help to run this command again

install
nvm install node
npm --version

use another global prefix

To avoid getting :

install
1
2
3
npm install -g protoc-gen-ts
npm ERR! code EACCES
...

You getter change the prefix to one you can write to ! For instance ~/.npm

config
1
2
3
4
npm config set prefix ~/.npm
npm install -g protoc-gen-ts
protoc-gen-ts
bash: protoc-gen-ts: command not found

Ok, it installs but your new prefix is not in your path, simply add this to your PATH in ~/.bashrc or ~/bin/kees.sh :

set path
PATH=$PATH:~/.npm/bin

Now you don't need to use sudo anymore.

npm

package.json

Later.. but know that using --save stores an entry in this file and you can steer versions in this file and then just run :

install
npm install

loglevel and warnings

One of the eyesores about npm is warnings like this :

warnings
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

This is ugly and totally unnecessary, because it is only needed on osx as the warning clarifies. So i don't want to see warnings, this can be steered with the --loglevel=error commandline option. That's a bit long to type each time. You could also use -s because that means --loglevel=silent, but i don't know if that suppresses errors as well.

Better

config list
npm config ls -l | grep log

This will print the currently set options. You can see what exactly a parameter should be, in this case :

loglevel
loglevel = "notice"
logs-max = 10

You can use the 'ole' way of rc files to change commandline options. An easy way to do this is to use npm config.

loglevel
npm config set loglevel error

This will create the file ~/.npmrc with these contents.

~/.npmrc
loglevel=error

So yes you could do that by hand, but this guarantees the correct filename and syntax. If you want it on a per-package base, you should put a .npmrc in the current directory which will override the global one in your home directory. In fact the npm config reports the decisions about overrides. Here is the output when you have a local .npmrc with loglevel=silent, and a global one with loglevel-error:

view loglevel
1
2
3
4
5
6
npm config ls -l | grep log

loglevel = "silent"
; loglevel = "error" (overridden)
; loglevel = "notice" (overridden)
logs-max = 10

npm audit

This command runs a security audit on your project, and also all used packages. When you run npm audit you will get a list of vulnerabilities, and you can run a fix to fix them :

audit
1
2
3
npm audit
npm audit fix
npm adith fix --force

The last one is needed if there are fixes that cannot be installed without force. But still --force will fail for some updates. For instance :

audit
=== npm audit security report ===                        

┌──────────────────────────────────────────────────────────────────────────────┐
                                Manual Review                                             Some vulnerabilities require your attention to resolve                                                                                                   Visit https://go.npm.me/audit-guide for additional guidance          └──────────────────────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
 Moderate       Command Injection                                            ├───────────────┼──────────────────────────────────────────────────────────────┤
 Package        dot                                                          ├───────────────┼──────────────────────────────────────────────────────────────┤
 Patched in     No patch available                                           ├───────────────┼──────────────────────────────────────────────────────────────┤
 Dependency of  @compodoc/compodoc [dev]                                     ├───────────────┼──────────────────────────────────────────────────────────────┤
 Path           @compodoc/compodoc > @compodoc/ngd-transformer > dot         ├───────────────┼──────────────────────────────────────────────────────────────┤
 More info      https://npmjs.com/advisories/798                             └───────────────┴──────────────────────────────────────────────────────────────┘
found 1 moderate severity vulnerability in 27339 scanned packages
1 vulnerability requires manual review. See the full report for details.

You will have to decide on this report what to do.

npm and bootstrap

Error

91% additional chunk assets processingError: ENOENT: no such file or directory, open 'D:\ANGUALRJS2 PROJECTS\NG2CLIAPP01\src\node_modules\bootstrap\dist\js\bootstrap.min.js' #9438

Or similar message during bootstrap installation. The main problem is that some installations put the node_modules directory in the current directory, and some one level higher.

The example installation showed to import some .js and .css files from '../node_modules' while the current angular installation just seems to install all modules under './node_modules'.

This made clear which of the two is used :

clear and reinstall
1
2
3
rm -rf ./node_modules
rm -rf ../node_modules
npm install # created ./node_modules again

The entries in angular.json with ../node_modules al had to be changed and after that everything works again.

SyntaxError: Cannot use import statement outside a module

That error, when using import can't be understood by plain nodejs. Here is an example file that will fail :

wrong
1
2
3
import {Observable} from 'rxjs';

Observable.create() ...

import is an ECMAScript 6 directive, so you have to install and use it :

ecmascript
npm i esm
node -r esm example.js

Source: visit

The other way (CommonJS) is to user require :

commonjs
1
2
3
4
const rx = require('rxjs');
const Observable = rx.Observable;

Observable.create()...

Now you don't need the -r esm option.

Sometimes you see this syntax, but i have not found how that is supposed to work ?!?

rxjs
1
2
3
4
const Observable = rxjs;

// always gives : 
ReferenceError: rxjs is not defined