How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)

前端 未结 28 2804
难免孤独
难免孤独 2020-11-21 05:28

My version of node is always v0.6.1-pre even after I install brew node and NVM install v0.6.19.

My node version is:



        
28条回答
  •  孤城傲影
    2020-11-21 06:13

    Docker - alternative approach

    Docker is some-kind of super-fast virtual machine which can be use to run tools like node (instead install them directly on mac-os). Advantages to do it are following

    • all stuff ('milions' node files) are install inside docker image/container (they encapsulated in few inner-docker files)

    • you can map your mac directory with project to your docker container and have access to node - but outside docker, mac-os sytem don't even know that node is installed. So you get some kind of 'virtual' console with available node commands which can works on real files

    • you can easily kill node by find it by docker ps and kill by docker rm -f name_or_num

    • you can easily uninstall docker image/containers by one command docker rmi ... and get free space - and install it again by run script (below)

    • your node is encapsulated inside docker and don't have access to whole system - only to folders you map to it

    • you can run node services and easily map they port to mac port and have access to it from web browser

    • you can run many node versions at the same time

    • in similar way you can install other tools like (in many versions in same time): php, databases, redis etc. - inside docker without any interaction with mac-os (which not notice such software at all). E.g. you can run at the same time 3 mysql db with different versions and 3 php application with different php version ... - so you can have many tools but clean system

    • TEAM WORK: such enviroment can be easily cloned into other machines (and even to windows/linux systems - with some modifications) and provide identical docker-level environment - so you can easily set up and reuse you scripts/dockerfiles, and setup environment for new team member in very fast way (he just need to install docker and create similar folder-structure and get copy of scripts - thats all). I work this way for 2 year and with my team - and we are very happy

    Instruction

    • Install docker using e.g. this instructions

    • Prepare 'special' directory for work e.g. my directory is /Users/kamil/work (I will use this directory further - but it can be arbitrary) - this directory will be 'interface' between docker containers and your mac file ststem. Inside this dir create following dir structure:

      /Users/kamil/work/code - here you put your projects with code

      /Users/kamil/work/tools

      /Users/kamil/work/tools/docker-data - here we map containers output data like logs (or database files if someone ouse db etc.)

      /Users/kamil/work/tools/docker

      /Users/kamil/work/tools/docker/node-cmd - here we put docker node scripts

    • inside tools create file .env which will contain in one place global-paths used in other scripts

      toolspath="/Users/kamil/work/tools"
      codepath="/Users/kamil/work/code"
      workpath=/Users/kamil/work

    • innside dir ../node-cmd create file dockerfile with following content

      # default  /var/www/html (mapped to .../code folder with projects)
      FROM node
      
      WORKDIR /work
      
      # Additional arbitrary tools (ng, gulp, bower)
      RUN npm install -g n @angular/cli bower gulp grunt
      
      CMD while true; do sleep 10000; done
      
      # below ports are arbitrary
      EXPOSE 3002 3003 3004 4200

    • innside dir ../node-cmd create file run-container with following content (this file should be executable e.g. by chmod +x run-container) - (notice how we map port-s and directories form external 'world' to internal docker filesystem)

      set -e
      cd -- "$(dirname "$0")" # this script dir (not set on doubleclick)
      source ../../.env
      toolsdir=$toolspath/docker-data
      workdir=$workpath
      
      if [ ! "$(docker ps | grep node-cmd)" ] 
      then 
        docker build -t node-cmd .
        docker rm -f node-cmd |:
        docker run -d --name node-cmd -p 4200:4200 -p 4201:4201 -p 3002:3002 -p 3003:3003 -p 3004:3004 -v $toolsdir/node-cmd/logs:/root/.npm/_logs -v $workdir:/work node-cmd
      fi

    • ok now you can add some project e.g. work/code/myProject and add to it following file 'run-cmd' (must be executable)

      cd -- "$(dirname "$0")"
      ../../tools/docker/node-cmd/run-container
      docker exec -it node-cmd bash -c "cd /work/code/myProject; bash"

    • then if you run above script (by double-click), you will see console with available node commands in project directory e.g. npm install

    • to run project in background (e.g some serwice) e.g. run web-server angular-cli application you can use following script (named run-front -must be executable) - (you must also edit /etc/hosts file to add proper domain)

      cd -- "$(dirname "$0")"
      open "http://my-angular.local:3002"
      ../../tools/docker/node-cmd/run-container
      docker exec -it node-cmd  /bin/sh -c "cd /work/code/my-angular-project; npm start"
      cat         # for block script and wait for user ctrl+C

提交回复
热议问题