09 February 2017

NodeJs Notes

NodeJS notes for working with client side.

Installation/Setup Node.js

NPM is used for installing Node js modules i.e. client side or dev dependencies.

Install NodeJs

  • Windows: Open Command Prompt as admin in Project root.
    • Chocolatey: @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
    • Node.js NPM: choco install nodejs
  • Mac: Use the installer from Homebrew:
    • brew install node -without-npm
      NB: you need to use a script to set it up: (see this post and this npm bug)
ls -1 /usr/local/lib/node_modules > ~/node_modules.txt
sed -e "s,/\+$,," -e "s,^/\+,," ~/node_modules.txt
rm -rf /usr/local/lib/node_modules
brew uninstall node --force node
brew install node --without-npm
echo prefix=~/.node >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh
echo 'export PATH="$HOME/.node/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
for i in `cat ~/node_modules.txt`;do npm install -g $i;done
rm ~/node_modules.txtInitialise DotNet Project
Use Yoman to create new project
* npm install -g generator-aspnet
* yo aspnet

Initialise

* Node.js: to create project.json: npm init --yes

Install NodeJs Packages

* npm search [package name]
* npm install [package name] [--no-optional]

Add Client Side Dependencies

* [TypeScript](https://www.typescriptlang.org/): npm install typescript --save-dev

JSPM

[JavaScript Package Manager](http://jspm.io/docs/getting-started.html)
* npm install jspm --save-dev
* jspm init
* Install from jspm, github or npm:
  * jspm install npm:lodash-node
  * jspm install github:components/jquery
  * jspm install jquery
NB: Got github you can use any source like owner/repo:
jspm install bulma=github:jgthms/bulma

Typings

These have been replaced by e.g:
npm install --save @types/systemjs

Add Client Side Dependency

To add a new client side dependency e.g. requirejs:
* JSPM:
    * jspm search requirejs
    * jspm install requirejs --save
* Types:
    * npm install --save @types/systemjs

Update npm

https://docs.npmjs.com/misc/semver
npm update <package name> --save

Cleaning node_modules

Sometimes the node_modules folder in particular contains too many sub-folders and the resulting folder structure is too long for Windows Ui tools to delete. To overcome this you can use a third party tool like rimraf:
* cd to node_modules's parent directory
* npm install rimraf -g
* rimraf node_modules

Develpment on Different Platform

It's possible to run the NPM part of apps on Android:
* Install Termux from Google Play
* apt update && apt upgrade
* termux-setup-storage
* set the environment variables: TMPDIR, TMP or TEMP

No comments:

Post a Comment