tweeeetyのぶろぐ的めも

アウトプットが少なかったダメな自分をアウトプット<br>\(^o^)/

【node.js】npmで`SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode` というエラーで怒られる

はじめに

f:id:tweeeety:20180608202233p:plain

タイトルのまんまですが、
とあるプロジェクトでnpm runスクリプトを走らせると以下のようなエラーが出たのでその対処方メモ

$ npm run sample

> hoge@1.0.0 sample /Users/tweeeety/hoge
> gulp

/Users/tweeeety/hoge/node_modules/riot-compiler/node_modules/source-map/lib/source-map-generator.js:21
class SourceMapGenerator {
^^^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/tweeeety/hoge/node_modules/riot-compiler/node_modules/source-map/source-map.js:6:30)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)

npm ERR! Darwin 17.5.0
npm ERR! argv "/Users/tweeeety/.nodebrew/node/v5.12.0/bin/node" "/Users/tweeeety/.nodebrew/current/bin/npm" "run" "compile"
npm ERR! node v5.12.0
npm ERR! npm  v3.8.6
npm ERR! code ELIFECYCLE
npm ERR! hoge@1.0.0 compile: `gulp`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the hoge@1.0.0 compile script 'gulp'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the hoge package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     gulp
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs hoge
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls hoge
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/tweeeety/hoge/npm-debug.log

このときの環境

$ ruby -v
ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-darwin17]

$ node -v
v5.12.0

$ npm -v
3.8.6

$ rbenv -v
rbenv 1.1.1

$ nodebrew list
v5.12.0

current: v5.12.0

なにがおこったか

根本原因

githubのissueにあがってました

Version 0.7.0 of source-map fails to load on Node < 6 due to let without 'use strict'. This causes the following error:

node moduleのsource-map 0.7.0は
'use strict'を使用しないのでnodeのversion<6(6未満)にはロードできないらしい。
そのせいでエラーが発生するとのこと。

自分のところの原因

nodeが5.x.xでも、package.jsonでバージョンをがちがちに固定していれば大丈夫だと思います。

  • nodeが5.x.x
  • 固定していないmodule(今回でいうsource-map)のversionがあがって5.x.xでは動かなくなった

というかんじ

確認する

このプロジェクトで使っているsouce-mapを確認してみます。

# たしかに0.7.3がいる
$ npm list source-map
hoge@1.0.0 /Users/tweeeety/hoge
├─┬ gulp-concat@2.6.1
│ └─┬ concat-with-sourcemaps@1.1.0
│   └── source-map@0.6.1
├─┬ gulp-cssmin@0.2.0
│ └─┬ clean-css@3.4.28
│   └── source-map@0.4.4
├─┬ gulp-uglify@3.0.0
│ ├─┬ uglify-js@3.4.0
│ │ └── source-map@0.6.1
│ └─┬ vinyl-sourcemaps-apply@0.2.1
│   └── source-map@0.5.7
└─┬ riot@3.10.1
  └─┬ riot-compiler@3.5.1
    └── source-map@0.7.3

対応

記載の通りですが、
6以上にあげるとエラーはでなくなりました。

# インストールできるversionを確認
$ nodebrew ls-remote

# とりあえず少しだけあげて6.x.xにする
$ nodebrew install v6.14.2
$ nodebrew use v6.14.2
use v6.14.2

# エラーでない
$ npm run sample

ちなみに

5.x.xもものすっごく昔というわけではないですが、
nodeの5.x.xはサポートは終了しているようです。

特に理由がなさそうならあげちゃったほうがよさそうです。

おわり

5系は古い \(^o^)/

【node.js】node.jsのリリース状況やLTS(Long-Term-Support:長期サポート)やEOL(End-of-Life:サポート終了)を知る

はじめに

node.jsやその周辺のversionが合わなくて環境がおかしい...
そんなときにリリース状況やサポート状況を知りたい自分用メモです。

どこで知るか

以下で知れます

どんな感じか

この記事時点での、ですがこんな感じで見れます。

Release schedule

f:id:tweeeety:20180608180056p:plain

https://github.com/nodejs/Release#release-schedule

End-of-Life Releases

f:id:tweeeety:20180608180104p:plain

https://github.com/nodejs/Release#end-of-life-releases

おわり

意外に探そうと思った時ぐぐってもぱっと出てこない自分がいます...w
\(^o^)/

シェルスクリプトやbashでset -eしたまま処理を中断せずに続けるメモ

はじめに

シェルスクリプトで意図せずエラー時に処理を中断させたい場合にset -eを使います。

set -eしつつ、意図したエラーであれば処理を先に進めたい場合のメモです。

アジェンダ

  • どうやるか
  • 具体例
  • 使い所

どうやるか

&& true を使います。

いくつか方法はあるのですが、自分的にはこれだけ使ってます。

具体例

サンプル

以下のようなシェルスクリプトサンプルで説明します。

  • stop.sh
$ cat stop.sh
#!/bin/bash

set -e

echo "hoge" # あってる
ehco "fuga" # echoをスペルミス
echo "piyo" # あってる
  • そのまま実行した場合
$ ./stop.sh
hoge
./stop.sh: line 4: ehco: command not found

piyoが出力されずに処理が中断されます。

&& trueを使う

  • stop.shを以下のように変更します
$ cat stop.sh
#!/bin/bash

set -e

echo "hoge" && true
ehco "fuga" && true
echo "piyo" && true
  • 実行
$ ./stop.sh
hoge
./stop.sh: line 6: ehco: command not found
piyo

piyoまで出力されました。

おわり

ちょいスクリプトにこそエラーハンドリングがっつり書くよりはset -eですませたい。
でもここで処理は中断したくない!ってことはまぁまぁあります。
横着しすぎ説\(^o^)/

シェルスクリプトやbashの処理を中断する3パターンの`-e` - とアンチパターン

はじめに

シェルスクリプトを書いていて
予想外の失敗時に処理が先に進まないでほしい
場合に処理を中断してほしいことがありますよね。

そんなときのメモ

アジェンダ

どうやるか

-eオプションをつけます。

-eをつけることで、exit 0(正常ステータス)
以外が返る場合に処理を中止するようになります。

どう使うか

-eオプションでの実行は何パターンかあります

  1. bashコマンドのオプションにつけてシェルスクリプトを実行する
  2. シェルスクリプトshebangにつける
  3. シェルスクリプト内で"set -e"を呼ぶ

具体例

サンプル

以下のようなシェルスクリプトサンプルで説明します。

  • stop.sh
$ cat stop.sh
#!/bin/bash

echo "hoge" # あってる
ehco "fuga" # echoをスペルミス
echo "piyo" # あってる
  • そのまま実行した場合
$ ./stop.sh
hoge
./stop.sh: line 4: ehco: command not found
piyo

line 4: ehco: command not found とは表示されますが、
piyoまで出力されています。

1. bashコマンドのオプションにつけてシェルスクリプトを実行する

シェルスクリプトの実行時にbashコマンド + -eで実行します

$ bash -e stop.sh
hoge
stop.sh: line 4: ehco: command not found

piyoが出力されずに処理が中断されました。

2. シェルスクリプトshebangにつける

シェルスクリプトの冒頭に記載する#!/bin/bash-eをつけます

  • stop.shを以下のように変更します
$ cat stop.sh
#!/bin/bash -e

echo "hoge" # あってる
ehco "fuga" # echoをスペルミス
echo "piyo" # あってる
  • 実行
$ ./stop.sh
hoge
./stop.sh: line 4: ehco: command not found

piyoが出力されずに処理が中断されました。

3. シェルスクリプト内で"set -e"を呼ぶ

シェルスクリプトの冒頭でset -eします。

  • stop.shを以下のように変更します
$ cat stop.sh
#!/bin/bash

set -e

echo "hoge" # あってる
ehco "fuga" # echoをスペルミス
echo "piyo" # あってる
  • 実行
$ ./stop.sh
hoge
./stop.sh: line 4: ehco: command not found

アンチパターン

しかし、注意することがあります。

2. シェルスクリプトのshebangにつけるbashコマンドで呼び出すとうまくいきません。

  • stop.sh
$ cat stop.sh
#!/bin/bash -e

echo "hoge" # あってる
ehco "fuga" # echoをスペルミス
echo "piyo" # あってる
  • 実行
# そのまま実行
$ ./stop.sh
hoge
./stop.sh: line 4: ehco: command not found

# bashコマンドで呼び出し
# piyoまで出力されてしまう
$ bash ./stop.sh
hoge
./stop.sh: line 4: ehco: command not found
piyo

これだと意図せず中断したいから大丈夫だろうと思いつつ
意図せず続いてしまうのでだいぶむしろ怖いです。

3. シェルスクリプト内で"set -e"を呼ぶのほうは大丈夫です

$ cat stop.sh
#!/bin/bash

set -e

echo "hoge" # あってる
ehco "fuga" # echoをスペルミス
echo "piyo" # あってる

$ ./stop.sh
./stop.sh
hoge
./stop.sh: line 6: ehco: command not found

$ bash stop.sh
hoge
stop.sh: line 6: ehco: command not found

合わせて

おわり

set -eは若干の賛否両論はありますが、 意図せず処理を続けることがないようにしたい場合は
シェルスクリプト内にset -eするのが安心です\(^o^)/

【node.js】macにnodebrewでnode.jsのバージョン管理を行う爆速あんちょこ

はじめに

あたらしいmac環境を手に入れたので
nodebrewでnode.jsを管理する環境を作ります。

復習もかねての自分用あんちょこメモです

アジェンダ

  1. nodebrewとは
  2. node.jsの削除
  3. nodebrewのインストール
  4. nodebrewのsetup
  5. node.jsのインストール
  6. node.jsの切り替え

1. nodebrewとは

Node.js version manager.

https://github.com/hokaccha/nodebrew

公式サイトに書かれている説明です。

Node.jsを自分のマシン内でバージョン管理するためのToolです。

2. node.jsの削除

なにで入れたかによって違いますが、
nodebrewでのnode.js管理をする場合はそれ以外で入れたものは削除しておきます。

以下はhomebrewで入れた場合の削除例です

$ brew uninstall node

3. nodebrewのインストール

$ brew install nodebrew

4. nodebrewのsetup

nodebrew setupと打つだけです。

# setupを行う
# $HOME/.nodebrewが作られる
$ nodebrew setup
Fetching nodebrew...
Installed nodebrew in $HOME/.nodebrew

========================================
Export a path to nodebrew:

export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================

# 確認
$ ls -ld .node*
drwxr-xr-x  9 tweeeety  tweeeety  288  6  6 21:32 .nodebrew

setupが終わると出力されるとおり、PATHの追加を忘れずに。

$ echo "export PATH=$HOME/.nodebrew/current/bin:$PATH" >> ~/.bashrc

5. node.jsのインストール

インストール可能versionの確認

インストール可能versionはnodebrew ls-remoteで確認できます

$ nodebrew ls-remote
v0.0.1    v0.0.2    v0.0.3    v0.0.4    v0.0.5    v0.0.6

v0.1.0    v0.1.1    v0.1.2    v0.1.3    v0.1.4    v0.1.5    v0.1.6    v0.1.7
v0.1.8    v0.1.9    v0.1.10   v0.1.11   v0.1.12   v0.1.13   v0.1.14   v0.1.15

~ 省略

io@v2.0.0 io@v2.0.1 io@v2.0.2 io@v2.1.0 io@v2.2.0 io@v2.2.1 io@v2.3.0 io@v2.3.1
io@v2.3.2 io@v2.3.3 io@v2.3.4 io@v2.4.0 io@v2.5.0

io@v3.0.0 io@v3.1.0 io@v3.2.0 io@v3.3.0 io@v3.3.1

node.jsのインストール

主に以下のコマンドでインストールします

  • nodebrew install [version]
    • 任意のversionをインストール
  • nodebrew install-binary stable
    • 安定版をインストール
  • nodebrew install-binary latest
    • 最新版をインストール
# 任意のバージョンをインストール
$ $ nodebrew install v5.12.0
Fetching: https://nodejs.org/dist/v5.12.0/node-v5.12.0-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully

# 安定版をインストール
$ nodebrew install-binary stable
Fetching: https://nodejs.org/dist/v10.3.0/node-v10.3.0-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully

6. node.jsの切り替え

nodebrew user [version]で指定して切り替えます。

# 利用可能バージョン確認
# currentが指定ないのでどれも選択していない
$ nodebrew list
v5.12.0
v10.3.0

current: none

# この状態ではnodeコマンドもない
$ node -v
-bash: node: command not found

# バージョンを指定
$ nodebrew use v5.12.0

# 指定したバージョンで利用可能になる
$ node -v
v5.12.0

$ npm -v
3.8.6

# listでも確認
$ nodebrew list
v5.12.0
v10.3.0

current: v5.12.0

おわり

さくっできて簡単\(^o^)/

【ruby】macでrbenv x bundlerでgemを管理するまでの爆速あんちょこ

はじめに

あたらしいmac環境を手に入れたので
rbenv x bundlerでgemを管理する環境を作ります。

復習もかねての自分用あんちょこメモです

あじぇんだ

  1. rbenvインストール
  2. rbenvでのrubyインストールと切り替え
  3. bundlerインストール
  4. gemのインストール

前提というほどではないですが、homebrewなどは使える状態を想定してます。

1. rbenvインストール

$ brew install rbenv

$ rbenv -v
rbenv 1.1.1

PATHの追加

~/.bashrcあたりに以下を追加します

export PATH="$HOME/.rbenv/shims:$PATH"

if type rbenv >/dev/null 2>&1; then
  # ${HOME}/.rbenvが作られる
  eval "$(rbenv init -)"and to continue
fi

2. rbenvでのrubyインストールと切り替え

任意のversionをインストール

# 初期状態
$ rbenv versions
* system (set by /usr/local/var/rbenv/version)

# インストール可能versionの表示
$ rbenv install --list
Available versions:
  1.8.5-p52
  1.8.5-p113
  1.8.5-p114
  1.8.5-p115
  1.8.5-p231

~ 省略 ~

  rbx-3.99
  rbx-3.100
  ree-1.8.7-2011.03
  ree-1.8.7-2011.12
  ree-1.8.7-2012.01
  ree-1.8.7-2012.02
  topaz-dev

# リストの中から任意のversionをインストール
$ rben install 2.4.4

# いくつかインストールしてから確認
$  rbenv versions
* system (set by /Users/tweeeety/hoge/.ruby-version)
  2.4.0
  2.4.2
  2.4.4

versionの切り替え

切り替えには主に以下の方法を使う

  • rbenv global [バージョン]
    • ~/.rbenv/version に書き込まれる
  • rbenv local [バージョン]
    • ~/[rbenb localしたディレクトリ]/.ruby-version

実際にやるとこんなん

# rubyのversion確認
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]

# このときのrbenv versions
$ rbenv versions
* system (set by /Users/tweeeety/hoge/.ruby-version)
  2.4.0
  2.4.2
  2.4.4

# 切り替え
$ rbenv local 2.4.4

# rubyのversion確認
$ ruby -v
ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-darwin17]

# このときのrbenv versions
$ rbenv versions
  system
  2.4.0
  2.4.2
* 2.4.4 (set by /Users/tweeeety/hoge/.ruby-version)

3. bundlerインストール

bundler自体もgemです。
bundlerだけはグローバルにインストールします。

インストール

$ gem install bundler

$ which bundle
/Users/tweeeety/.rbenv/shims/bundle

設定

# 
$ bundler init

# Gemfileができる
$ ls
Gemfile

# 中身はこんなん
$ cat Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "rails"

4. gemのインストール

Gemfileに記載されたgemをインストール

$ bundle install --path vendor/bundle

おわり

特にはまることもないのでさくっといけます\(^o^)/

【git】gitコマンドで毎回"Enter passphrase for key ~"と聞かれる - ssh-add

はじめに

久しぶりに新PCに環境を作っていると
git cloneで毎回パスワードを聞かれるようになりました。

$ git clone git@github.com:tweeeety/hoge.git
Cloning into 'hoge'...
Enter passphrase for key '/Users/tweeeety/.ssh/id_rsa_github':

初歩的な事だけど何度か忘れてるのでそんな自分のためにメモ

前提

前提として他の設定はうまくいってます

  • githubsshのkey設定などは済んでる
  • ~/.ssh配下のpermissionなども正しい

解決

# git cloneで毎回パスワード聞かれる
$ git clone git@github.com:tweeeety/hoge.git
Cloning into 'hoge'...
Enter passphrase for key '/Users/tweeeety/.ssh/id_rsa_github':

# ssh-addする
$ ssh-add ~/.ssh/id_rsa_github
Enter passphrase for /Users/tweeeety/.ssh/id_rsa_github_my:
Identity added: /Users/tweeeety/.ssh/id_rsa_github_my (/Users/tweeeety/.ssh/id_rsa_github_my)

# 再度git cloneする
$ git clone git@github.com:tweeeety/hoge.git
loning into 'hoge'...
remote: Counting objects: 17, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 17 (delta 2), reused 17 (delta 2), pack-reused 0
Receiving objects: 100% (17/17), done.
Resolving deltas: 100% (2/2), done.

おわり

すごく初歩的だけどたまに環境構築しなおしたりするとハマりますね