node-addon-example

遇到node-sqlit需要使用到arm上面
這邊了解一下nvm及node的c++如何compile


更新記錄

item note
20160830 第一版

目錄


NVM

  • Node Version Manage
    需要在同一電腦上面做不同板本的node切換,此時就需要用到nvm

如何安裝

  • 使用wget如下

    1
    wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.6/install.sh | bash
  • 安裝完成會在使用者目錄產生.nvm目錄

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    root@dc030135983a:~# ls -la
    total 32
    drwx------ 5 root root 4096 Aug 30 06:53 .
    drwxr-xr-x 65 root root 4096 Aug 30 05:40 ..
    -rw-r--r-- 1 root root 3301 Aug 30 06:53 .bashrc
    -rw-rw-r-- 1 root root 196 Mar 30 02:39 .gitconfig
    drwxr-xr-x 3 root root 4096 Aug 30 05:45 .node-gyp
    drwxr-xr-x 123 root root 4096 Aug 30 05:40 .npm
    drwxr-xr-x 4 root root 4096 Aug 30 06:53 .nvm
    -rw-r--r-- 1 root root 140 Apr 19 2012 .profile
  • 若建入nvm無反應,則先source .nvm/nvm.sh,就可以使用nvm指令

    1
    . .nvm/nvm.sh
  • example

    1
    2
    3
    4
    5
    6
    Example:
    nvm install v0.10.32 Install a specific version number
    nvm use 0.10 Use the latest available 0.10.x release
    nvm run 0.10.32 app.js Run app.js using node v0.10.32
    nvm exec 0.10.32 node app.js Run `node app.js` with the PATH pointing to node v0.10.32
    nvm alias default 0.10.32 Set default node version on a shell

使用nvm安裝node特定版本

  • nvm ls-remote
    List remote versions available for install
    查看可以使用node版本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    root@dc030135983a:~# nvm ls-remote
    xxx
    v0.10.43
    v0.10.44
    v0.10.45
    v0.10.46
    v0.11.0
    v0.11.1
    v0.11.16
    v0.12.0
    v0.12.1
    v0.12.2
    xx
    v4.1.2
    v4.2.0 (LTS: Argon)
    v4.2.1 (LTS: Argon)
    xx
  • 安裝特定版本

    1
    nvm install v0.10.43
  • test

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    root@db7e2830c910:~# nvm list
    v0.10.43
    -> v0.12.1
    default -> v0.10.43
    node -> stable (-> v0.12.1) (default)
    stable -> 0.12 (-> v0.12.1) (default)
    iojs -> N/A (default)
    lts/* -> lts/argon (-> N/A)
    lts/argon -> v4.5.0 (-> N/A)
    root@db7e2830c910:~# node -v
    v0.12.1
  • 切換到其它版本

    1
    2
    3
    4
    root@db7e2830c910:~# nvm use v0.10.43
    xx
    root@db7e2830c910:~# node -v
    v0.10.43

如何build addon c++

example / 1_hello_world

  • node-addon-examples
  • 於1_hello_world/node_0.10目錄內
  • 安裝build code需要的gyp工具

    1
    sudo npm install node-gyp -g
  • 安裝此package需要的相關套件

    1
    npm install
  • node-gyp rebuild

binding.gyp

如何cross compile arm

  • 需要先export用到的env

    1
    2
    3
    4
    export AR=/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/bin/arm-hisiv200-linux-ar
    export CC=/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/bin/arm-hisiv200-linux-gcc
    export CXX=/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/bin/arm-hisiv200-linux-g++
    export LINK=/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/bin/arm-hisiv200-linux-g++
  • 指定cpu為arch為arm且build

    1
    node-gyp --arch arm configure build

遇到問題

目前使用docker ubuntu 12.04 32bit

  • 使用nvm install v0.10.43之後

    1
    2
    3
    4
    5
    6
    root@01da8dacc643:~# nvm install v0.10.43
    ######################################################################## 100.0%
    /root/.nvm/v0.10.43/bin/npm: line 2: /root/.nvm/v0.10.43/bin/node: No such file or directory
    nvm is not compatible with the npm config "prefix" option: currently set to ""
    Run `npm config delete prefix` or `nvm use --delete-prefix v0.10.43` to unset it.
    root@01da8dacc643:~# ls -l /root/.nvm
  • 發現nvm安裝64bit node,,怪怪的

    1
    2
    root@db7e2830c910:~# file .nvm/v0.10.43/bin/node
    .nvm/v0.10.43/bin/node: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped

改為使用docker ubuntu 14.04 64bit

  • docker 64bit os
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    root@db7e2830c910:~# nvm install v0.10.43                                                                                                                     

    --2016-08-30 08:35:07-- https://nodejs.org/dist/v0.10.43/node-v0.10.43-linux-x64.tar.gz
    Resolving nodejs.org (nodejs.org)... 104.20.22.46, 104.20.23.46, 2400:cb00:2048:1::6814:172e, ...
    Connecting to nodejs.org (nodejs.org)|104.20.22.46|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 6085592 (5.8M) [application/gzip]
    Saving to: '/root/.nvm/bin/node-v0.10.43-linux-x64/node-v0.10.43-linux-x64.tar.gz'

    100%[===============================================================================================================================================>] 6,085,5
    92 1.10MB/s in 7.4s

    2016-08-30 08:35:15 (803 KB/s) - '/root/.nvm/bin/node-v0.10.43-linux-x64/node-v0.10.43-linux-x64.tar.gz' saved [6085592/6085592]

    npm WARN deprecated This version of npm lacks support for important features,
    npm WARN deprecated such as scoped packages, offered by the primary npm
    npm WARN deprecated registry. Consider upgrading to at least npm@2, if not the
    npm WARN deprecated latest stable version. To upgrade to npm@2, run:
    npm WARN deprecated
    npm WARN deprecated npm -g install npm@latest-2
    npm WARN deprecated
    npm WARN deprecated To upgrade to the latest stable version, run:
    npm WARN deprecated
    npm WARN deprecated npm -g install npm@latest
    npm WARN deprecated
    npm WARN deprecated (Depending on how Node.js was installed on your system, you
    npm WARN deprecated may need to prefix the preceding commands with `sudo`, or if
    npm WARN deprecated on Windows, run them from an Administrator prompt.)
    npm WARN deprecated
    npm WARN deprecated If you're running the version of npm bundled with
    npm WARN deprecated Node.js 0.10 LTS, be aware that the next version of 0.10 LTS
    npm WARN deprecated will be bundled with a version of npm@2, which has some small
    npm WARN deprecated backwards-incompatible changes made to `npm run-script` and
    npm WARN deprecated semver behavior.
    Now using node v0.10.43 (npm v1.4.29)
    Creating default alias: default -> v0.10.43
    root@db7e2830c910:~# ls -la

其它參考資料