npm searchで「JavaScript heap out of memory」が出た時の対処方法

# npm searchで「JavaScript heap out of memory」が出た時の対処方法

## :hourglass: 現象
今まで、Windowsで作業していてのですが、macにも同じ環境で作業したかったので、Node.jsのcurrentバージョン(v6.4.0)を今日入れました。

バージョン指定で試したかったので、brewからnodebrewを使用して、インストールを行い、nmp searchを実行したところ、JavaScriptのヒープメモリエラーになり、実行できない現象が発生したのでその対処法です。

## :computer: 環境

- Mac OS X
    - v10.11.6 EI Capitan
- Node.js
    - v6.4.0
- npm
    - v3.10.3

## :mag_right: 対処法

nodeコマンドに対して、パラメータを指定して、aliasを作成して、実行をしました。[^github]

```sh
$ alias npm8gb='node --max-old-space-size=8192 ~/.nodebrew/current/bin/npm'
$ npm8gb search datajs
```

### エラー内容

```sh
$ npm search datajs
npm WARN Building the local index for the first time, please be patient
⸨░░░░░░░░░░░░░░░░░░⸩ ⠋ : WARN Building the local index for the first time, please be patient
<--- Last few GCs --->

   30381 ms: Mark-sweep 924.9 (1225.8) -> 923.5 (1233.8) MB, 450.7 / 0 ms (+ 212.1 ms in 1148 steps since start of marking, biggest step 9.6 ms) [allocation failure] [GC in old space requested].
   31220 ms: Mark-sweep 923.5 (1233.8) -> 898.1 (1215.1) MB, 839.2 / 0 ms [allocation failure] [GC in old space requested].
   32073 ms: Mark-sweep 898.1 (1215.1) -> 898.0 (1207.1) MB, 852.4 / 0 ms [last resort gc].
   32893 ms: Mark-sweep 898.0 (1207.1) -> 897.9 (1201.1) MB, 820.3 / 0 ms [last resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x1eab77ec9e31 <JS Object>
    1: fromString(aka fromString) [buffer.js:194] [pc=0x37f488bca710] (this=0x1eab77e04189 <undefined>,string=0x154ba4f2cf61 <Very long string[193413690]>,encoding=0x1eab77ed7a41 <String[4]: utf8>)
    2: from [buffer.js:94] [pc=0x37f488b3e0d6] (this=0x3af851ea7ad1 <JS Function Buffer (SharedFunctionInfo 0x1eab77ee3319)>,value=0x154ba4f2cf61 <Very long string[193413690]>,encodingOrOffset=0x1eab...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/Users/rma/.nodebrew/current/bin/node]
 2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/Users/rma/.nodebrew/current/bin/node]
 3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/Users/rma/.nodebrew/current/bin/node]
 4: v8::internal::Factory::NewRawTwoByteString(int, v8::internal::PretenureFlag) [/Users/rma/.nodebrew/current/bin/node]
 5: v8::internal::String::SlowFlatten(v8::internal::Handle<v8::internal::ConsString>, v8::internal::PretenureFlag) [/Users/rma/.nodebrew/current/bin/node]
 6: v8::String::WriteUtf8(char*, int, int*, int) const [/Users/rma/.nodebrew/current/bin/node]
 7: node::StringBytes::Write(v8::Isolate*, char*, unsigned long, v8::Local<v8::Value>, node::encoding, int*) [/Users/rma/.nodebrew/current/bin/node]
 8: node::Buffer::New(v8::Isolate*, v8::Local<v8::String>, node::encoding) [/Users/rma/.nodebrew/current/bin/node]
 9: node::Buffer::CreateFromString(v8::FunctionCallbackInfo<v8::Value> const&) [/Users/rma/.nodebrew/current/bin/node]
10: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [/Users/rma/.nodebrew/current/bin/node]
11: v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::(anonymous namespace)::BuiltinArguments<(v8::internal::BuiltinExtraArguments)1>) [/Users/rma/.nodebrew/current/bin/node]
12: v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) [/Users/rma/.nodebrew/current/bin/node]
13: 0x37f48830961b
Abort trap: 6
```

## 参考
[^github]: github上にISSUEとして、上がっている。 https://github.com/npm/npm/issues/12619