NPM tips

npm install

different with ^:

1
2
3
4
5
6
$ npm install express@^4.16.1 --save --save-exact
+ express@4.17.1
removed 3 packages, updated 1 package and moved 1 package in 124.659s
$ npm install express@4.16.1 --save
+ express@4.16.1
added 4 packages from 4 contributors, removed 3 packages and updated 16 packages in 20.374s

ref: https://docs.npmjs.com/cli/install

npm list

check the installed packages under current folder:

1
2
3
4
5
6
7
$ npm list
express-test@0.0.0 C:\Users\eshibij\Desktop\express-test
-- express@4.16.1 invalid
+-- accepts@1.3.7 extraneous
+-- array-flatten@1.1.1 extraneous
+-- body-parser@1.18.2 extraneous
....

upgrade node to given version

linux

  1. install n widget as tool upgrading node:

    1
    2
    3
    4
    root@f7748a275a7a:/# node -v
    v14.5.0
    root@f7748a275a7a:/# npm cache clean -f
    npm WARN using --force I sure hope you know what you are doing.
    1
    2
    3
    4
    root@f7748a275a7a:/# npm install -g n        
    /usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
    + n@6.7.0
    added 1 package from 4 contributors in 0.205s
  2. install node with given version:

    1
    2
    3
    4
    5
    root@f7748a275a7a:/# n v14.6.0
    installing : node-v14.6.0
    mkdir : /usr/local/n/versions/node/14.6.0
    fetch : https://nodejs.org/dist/v14.6.0/node-v14.6.0-linux-x64.tar.xz
    installed : v14.6.0 (with npm 6.14.6)
    1
    2
    root@f7748a275a7a:/# node -v
    v14.6.0

    windows

    use nvm to manage node version on windows

  3. install nvm:
    download nvm-setup.zip from https://github.com/coreybutler/nvm-windows/releases then install

  4. check and switch node version:
    check installed node version:

    1
    $ nvm ls

    install node with given version:

    1
    $ nvm install vXX

    choose certain installed verision:

    1
    $ nvm use xxx

    if error occurs like:

    1
    2
    3
    4
    $ nvm install v10.15
    10.15.0
    Could not retrieve https://nodejs.org/dist/latest/SHASUMS256.txt.
    Get https://nodejs.org/dist/latest/SHASUMS256.txt: dial tcp 104.20.22.46:443: i/o timeout

    find nvm location with where nvm and modify the settings.txt under the path, add:

    1
    2
    node_mirror:npm.taobao.org/mirrors/node/
    npm_mirror:npm.taobao.org/mirrors/npm/

    node js run unit test (mocha)

    ref: https://blog.csdn.net/weixin_42429288/article/details/97638626

  5. run single test
    Add only before target case or suite, like

    1
    2
    3
    4
    5
    6
    7
    8
    9
    it.only('XXX-1035 ....', async () => {
    await db.initDB();
    await Area.dumpTestData2('../../test/UT/data/Area1.json');
    await agent.get('/test/v1/xxx?origin=xxxx1')
    .expect(200,[ { out: 2,
    in: 1,
    region: 'region1',
    adminZones: [ 'Zona Norte', 'Madrid Captial' ] } ])
    });

    can also add it before a suite, like:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    describe.only('#digestAPI test()', async () => {
    before(async () => {
    await db.initDB();
    const express = require('express');
    app = express();
    serverDigest.baseRoute(app,9999);
    agent = request.agent(app);
    });

    after(async function () {
    // process.exit();
    });

    it('XXX-980 ...', async () => {
    ...
    }
    }
  6. skip single Test
    same way as only, you can add it after describe or it to skip certain suite/case.

npm ERR! Maximum call stack size exceeded

Error came out when run npm install --save --save-exact react-scripts@4.0.1
Solution:

  1. upgrade npm to a newer version, it worked for me after I switching to npm v6.14.6 from npm v6.4.1
  2. delete package-lock.json and run npm rebuild, if not work, delete the npm_modules folder.

Node Sass does not yet support your current environment

1
2
3
4
5
6
7
8
9
$ npm run build
> my-cv@0.1.0 build C:\Users\...\Desktop\...\cv\my-cv
> react-scripts build
Creating an optimized production build...
Failed to compile.
./src/pages/App.module.scss
Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (83)
For more information on which environments are supported please see:
https://github.com/sass/node-sass/releases/tag/v4.13.0

Solution:

  1. uninstall node-sass: npm uninstall --save node-sass
  2. reinstall node-sass: npm install --save node-sass

Update:
new issue occur: Error: Node Sass version 5.0.0 is incompatible with ^4.0.0.
Solution:

  1. uninstall node-Sass
  2. install node-sass version before v5.0.0: npm install --save node-sass@4.14.1

upgrade globally installed npm packages

check out-dated packages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ npm outdated -g
Package Current Wanted Latest Location
@uisdk/cdt-build 2.5.10 2.6.1 2.6.1 global
@uisdk/cdt-package 2.5.4 2.5.8 2.5.8 global
@uisdk/cdt-serve 2.3.21 2.3.24 2.3.24 global
@uisdk/cdt-skeleton 2.3.2 2.3.3 2.3.3 global
@uisdk/cdt2 2.6.0 2.6.3 2.6.3 global
cnpm 6.1.0 6.1.1 6.1.1 global
eslint 6.0.1 6.8.0 7.18.0 global
gatsby-cli 2.17.1 2.18.0 2.18.0 global
hexo-cli 2.0.0 2.0.0 4.2.0 global
jshint 2.10.2 2.12.0 2.12.0 global
n 6.7.0 6.8.0 7.0.0 global
npm 6.14.6 6.14.11 6.14.11 global

update all global packages:

1
npm update -g

update specific global package:

1
npm update -g <package_name>

react-scripts dependency error

error message:

1
2
react-scripts package provided by Create React App requires a dependency:
"babel-eslint": "^10.1.0"

I tried to update babel-eslint globally but seems it was not out-dated, not sure why react use this new version.
solution:

  1. Add .env file under project with the following statement:
    1
    SKIP_PREFLIGHT_CHECK=true
  2. Save .env and delete package-lock.json
  3. npm install -> npm start

cannot install chart.js from react-chartjs-2

compling failed like:

1
2
ERROR in ./~/react-chartjs-2/lib/index.js
Module not found: Error: Can't resolve 'chart.js' in 'node_modules/react-chartjs-2/es'

The reason is that, chart.js is a peer dependancy. we should include a version in package.json.
But after running npm install --save chart.js, the react-chartjs-2 module got error:

1
2
3
4
5
6
7
8
TypeError: Cannot read property 'defaults' of undefined
(anonymous function)
C:/Users//Desktop/covid19-tracker/node_modules/react-chartjs-2/es/index.js:643
640 | }(_react["default"].Component);
641 |
642 | exports.Scatter = Scatter;
> 643 | var defaults = _chart["default"].defaults;
644 | exports.defaults = defaults;

resolved with

1
2
3
4
5
# firstly uninstall chart.js v3
$ npm uninstall chart.js
# reinstall chart.js v2
$ npm i chart.js@2.9 --save
+ chart.js@2.9.4