node.js

Ubuntu (WSL) で環境構築

since 2018-08-16

Windows 10 (1803) + WSL Ubuntu 18.04.1 で環境構築したメモ。

手順を概説すると、Ubuntu パッケージの npm で n を入れて、その後でパッケージの npm と nodejs を削除している。

sudo apt install -y npm
sudo npm install n -g
sudo n stable
sudo apt-get purge -y nodejs npm
sudo n stable
sudo ln -sf /usr/local/bin/node /usr/bin/node
sudo npm update -g npm
sudo ln -sf /usr/local/bin/npm /usr/bin/npm

これで npx create-react-app my-app とかできる。続きは ReactJS に。

node.js 更新

since 2019-01-21

ひさしぶりに ReactJS create-react-app したら node が古いと言われた。

前述の環境構築だと sudo npm -g install node が失敗する。

> node@11.7.0 preinstall /usr/local/lib/node_modules/node
> node installArchSpecificPackage

Unhandled rejection Error: EACCES: permission denied, open '/home/nishimotz/.npm/_cacache/tmp/f59d954a'

npm ERR! cb() never called!

npm ERR! This is an error with npm itself. Please report this error at:
npm ERR!     <https://npm.community>

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/nishimotz/.npm/_logs/2019-01-21T06_18_04_483Z-debug.log
module.js:544
    throw err;
    ^

Error: Cannot find module 'node-linux-x64/package.json'
    at Function.Module._resolveFilename (module.js:542:15)
    at Function.resolve (internal/module.js:18:19)
    at ChildProcess.<anonymous> (/usr/local/lib/node_modules/node/node_modules/node-bin-setup/index.js:18:27)
    at ChildProcess.emit (events.js:159:13)
    at maybeClose (internal/child_process.js:943:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node@11.7.0 preinstall: `node installArchSpecificPackage`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node@11.7.0 preinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/nishimotz/.npm/_logs/2019-01-21T06_18_04_526Z-debug.log

最初にやった n stable を繰り返すと更新できる。

https://github.com/tj/n

$ sudo n stable

     install : node-v11.6.0
       mkdir : /usr/local/n/versions/node/11.6.0
       fetch : https://nodejs.org/dist/v11.6.0/node-v11.6.0-linux-x64.tar.gz
######################################################################## 100.0%
   installed : v11.6.0

下記で lts をインストールできる

$ sudo n lts

以下 2013年ごろの記事

macports だと:

sudo port install nodejs
sudo port install npm

jslint の派生版 jshint を入れる:

sudo npm install -g jshint

Ubuntu に入れる:

$ ./configure
$ make
$ sudo make install
# v0.10.22 を入れたら /usr/local/bin/npm も入っていた。  

パッケージで入れる:

  sudo apt-get install nodejs
  sudo apt-get install npm
  npm config set registry http://registry.npmjs.org/
  sudo npm install -g jshint

http://stackoverflow.com/questions/12913141/installing-from-npm-fails

vagrant@precise64 Ubuntu 12.04.4 だと nodejs も npm もバージョンが古いので注意。

$ node -v
v0.6.12
$ npm -v
1.1.4
$ jshint -v
jshint v2.4.3

xml パーサー

XML パーサーを探したら xml2js を見つけた:

http://d.hatena.ne.jp/zebevogue/20130308/1362707814

https://github.com/Leonidas-from-XIV/node-xml2js

>npm install -g xml2js
npm http GET https://registry.npmjs.org/xml2js
npm http 200 https://registry.npmjs.org/xml2js
npm http GET https://registry.npmjs.org/xml2js/-/xml2js-0.2.8.tgz
npm http 200 https://registry.npmjs.org/xml2js/-/xml2js-0.2.8.tgz
npm http GET https://registry.npmjs.org/sax
npm http 200 https://registry.npmjs.org/sax
npm http GET https://registry.npmjs.org/sax/-/sax-0.5.5.tgz
npm http 200 https://registry.npmjs.org/sax/-/sax-0.5.5.tgz
npm WARN package.json dateformat@1.0.2-1.2.3 No repository field.
npm WARN package.json istanbul@0.1.22 No repository field.
npm WARN package.json pause@0.0.1 No repository field.
npm WARN package.json policyfile@0.0.4 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
xml2js@0.2.8 C:\Users\nishimotz\AppData\Roaming\npm\node_modules\xml2js
└── sax@0.5.5

xml2json というものもあった:

https://github.com/buglabs/node-xml2json

Windows 8 に xml2json を入れようとして Windows SDK 7.1 Command Prompt から下記を実行したらうまくいった。

npm install -g xml2json

Windows SDK が動かないコマンドプロンプトだと node-expat で下記のエラーになる:

(前略)
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBu
ild.targets(297,5): warning MSB8003: Could not find WindowsSDKDir
 variable from the registry.  TargetFrameworkVersion or PlatformT
oolset may be set to an invalid version number. [C:\Users\nishimo
tz\AppData\Roaming\npm\node_modules\xml2json\node_modules\node-ex
pat\build\deps\libexpat\expat.vcxproj]
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\x64\M
icrosoft.Cpp.x64.targets(146,5): error MSB6006: "CL.exe" はコード
-10
73741701 を伴って終了しました。 [C:\Users\nishimotz\AppData\Roamin
g\npm\node
_modules\xml2json\node_modules\node-expat\build\deps\libexpat\exp
at.vcxproj]
gyp ERR! build error
gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.303
19\msbuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (C:\Program Files\nodejs
\node_modules\npm\node_modules\node-gyp\lib\build.js:267:23)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:98
:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_p
rocess.js:789:12)
gyp ERR! System Windows_NT 6.2.9200
gyp ERR! command "node" "C:\\Program Files\\nodejs\\node_modules\\
npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\nishimotz\AppData\Roaming\npm\node_modules\x
ml2json\node_modules\node-expat
gyp ERR! node -v v0.10.10
gyp ERR! node-gyp -v v0.9.6
gyp ERR! not ok
npm ERR! weird error 1
npm ERR! not ok code 0

jQuery

jQuery も Windows SDK が必要だった。。

C:\Program Files\Microsoft SDKs\Windows\v7.1>npm install -g jquery
(中略)
jquery@1.8.3 C:\Users\nishimotz\AppData\Roaming\npm\node_modules\jquery
├── location@0.0.1
├── navigator@1.0.1
├── xmlhttprequest@1.4.2
├── htmlparser@1.7.6
├── contextify@0.1.6 (bindings@1.1.1)
└── jsdom@0.2.19 (cssom@0.2.5, cssstyle@0.2.3, request@2.27.0)

NODE_PATH

Windows では NODE_PATH 環境変数の設定が必要という話

http://tmlife.net/programming/javascript/npm-node-js-require-module-memo.html

http://sucrose.hatenablog.com/entry/20120315/p1

set NODE_PATH=C:\Users\nishimotz\AppData\Roaming\npm\node_modules
nodejs.txt · 最終更新: 2022/01/11 20:12 by Takuya Nishimoto
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0