プログラマーのあああブログ

プログラミングなどなど (PHP, node, Golangとか書いてます)

Babelでコンパイルしたclassの "[class名] is not a constructor" エラーの対処

Babelでコンパイルしたモジュールをrequire()で使おうとしたら、以下のエラーメッセージがでました。

"[class名] is not a constructor"


例えばこんな風にclassを作ってBabelでコンパイルして

export default class dog {
...
}

そしてrequire() でモジュールを読み込んでインスタンスを生成すると

const Dog = require('dog');
const dog = new Dog();

//
"Dog is not a constructor"

"Dog is not a constructor" とエラーになります。

importを使ってたら問題ないので気が付つかなかった。


対処方法は以下のように default プロパティを使うか、

const Dog = require('dog').default;

または、Babelに babel-plugin-add-module-exports プラグインを入れてコンパイルすれば大丈夫です。

// .babelrc
{
  "presets": ["env"],
  "plugins": [
    "add-module-exports"
  ]
}




参考

Unexpected “Uncaught TypeError: XXX is not a constructor” errors with Babel and ES6

babel-plugin-add-module-exports

Ubuntu16.0.4 メールサーバー構築設定(バーチャルメールボックス) Postfix + Dovecot

さくらVPSでメールサーバーを構築したときの手順です。
下記バージョンで設定しました。

バーチャルメールボックスで

info@mail.example.com

上記のメールアドレスを作成します。
バーチャルメールボックスを使えばUbuntuのユーザーアカウントを追加しなくて、メールアドレスを追加することができます。


必要パッケージのインストール

sudo apt-get install postfix dovecot-core dovecot-imapd dovecot-pop3d

Postfixのインストール中 Configuration画面で「No Configuration」を選択



Postfixの設定 (SMTP送信メールサーバー)

Postfixの設定ファイルを作成していきます。
設定ファイルのサンプルが "/usr/share/postfix/main.cf.dist" にあるので、それをコピーしてきて編集していくのもいいです。私は新規で以下の設定ファイルを作成しました。

> sudo vi /etc/postfix/main.cf

## 基本設定 ###
# ホスト名指定
myhostname = mail.example.com

# ドメイン名指定
mydomain = example.com

# 受信を許可する宛先ドメイン名
mydestination =

# 外部からのメール受信を許可
inet_interfaces = all

# IPアドレスのプロトコル (IPV4のみ)
inet_protocols = ipv4

# 
mynetworks_style = host, localhost

# 
mynetworks = 127.0.0.0/8

# Maildir形式のメールボックス
home_mailbox = Maildir/

# メールサーバーソフト名の隠蔽化
smtpd_banner = $myhostname ESMTP unknown

# 送受信メールサイズ (10Mに制限)
message_size_limit = 10485760

# メールボックスサイズ (1Gに制限)
mailbox_size_limit = 1073741824

# 
alias_maps = hash:/etc/aliases

### sasl関連設定 ###
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname

### TLS関連設定###
smtp_tls_security_level = may
smtpd_tls_security_level = may
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# 中継に関する設定、不正中継をされないために設定
smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated reject_unauth_destination

### 仮想ドメイン/仮想ユーザの設定
virtual_mailbox_domains = mail.example.com
virtual_mailbox_base = /home/vmail
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_uid_maps = static:10000
virtual_gid_maps = static:10000


メール送信はOP25B対策のため25番ポートを利用せずに、サブミッションポート(587番)を利用します。 "/etc/postfix/master.cf" のコメントを解除していきます。

> sudo vi /etc/postfix/master.cf

submission inet n       -       n       -       -       smtpd
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=$mua_client_restrictions
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject
.
.
#smtps     inet  n       -       y       -       -       smtpd




Dovecotの設定 (受信メールサーバー)

> sudo vi /etc/dovecot/dovecot.conf

# IPv4のみListen
listen = *
> sudo vi /etc/dovecot/conf.d/10-master.conf

unix_listener /var/spool/postfix/private/auth {
  mode = 0666
  user = postfix
  group = postfix
}
> sudo vi /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:~/Maildir:INDEX=/home/vmail/%n
valid_chroot_dirs = /home/vmail
> sudo vi /etc/dovecot/conf.d/10-mail.conf

# メールディレクトリ指定
mail_location = maildir:~/Maildir:INDEX=/home/vmail/%n

# メールプロセスのchroot先指定
valid_chroot_dirs = /home/vmail
> sudo vi /etc/dovecot/conf.d/10-ssl.conf

# SSLを有効化
ssl = yes
# 証明書設定(Ubuntu標準のものを指定)
ssl_cert = </etc/ssl/certs/ssl-cert-snakeoil.pem
ssl_key = </etc/ssl/private/ssl-cert-snakeoil.key
# SSLプロコトル
ssl_protocols = !SSLv2
> sudo vi /etc/dovecot/conf.d/10-auth.conf

# PlainTextを無効化
disable_plaintext_auth = yes

# 認証メカニズムに scram-sha-1 を利用
auth_mechanisms = plain login scram-sha-1

# auth-system.conf.ext のインクルードを停止
#!include auth-system.conf.ext

# auth-passwdfile.conf.ext のインクルードを有効化
!include auth-passwdfile.conf.ext
> sudo vi /etc/dovecot/conf.d/auth-passwdfile.conf.ext

passdb {
  driver = passwd-file
  args = scheme=CRYPT username_format=%n /etc/dovecot/users
}

userdb {
  driver = passwd-file
  args = username_format=%n /etc/dovecot/users
}


imap/pop3認証用のSCRAM-SHA-1パスワードの生成

ここで入力したパスワードはメールソフトのアカウント認証で使います。

> sudo doveadm pw -s scram-sha-1

Enter new password:
Retype new password:
{SCRAM-SHA-1}xxxxxxxxxxx


imap/pop3認証用のパスワードファイルを作成

パスワードファイルの書式は以下になります。
ユーザー名:{SCRAM-SHA-1}xxxxxxxxxxx:uid:gid::メールボックスのパス

> sudo vi /etc/dovecot/users

info:{SCRAM-SHA-1}ea07d83fde6deee3...:10000:10000::/home/vmail/info


バーチャルメール用のアカウントとディレクトリ作成

所有者となるグループ、ユーザーの作成
グループ、ユーザー = vmail   UID、GID = 10000

sudo groupadd -g 10000 vmail
sudo useradd -u 10000 -g vmail -s /bin/false -d /home/vmail -m vmail

バーチャルメールディレクトリとアカウントディレクトリの作成

sudo mkdir /home/vmail
sudo chown vmail:vmail /home/vmail
sudo chmod 700 /home/vmail

sudo mkdir -p /home/vmail/info/Maildir/{new,cur,tmp}
sudo chown -R vmail:vmail /home/vmail/info


メールアドレスとディレクトリのマッピングファイル作成

メールアドレスとメールを保存するディレクトリとの対応ファイルを作成します。
書式は以下になります。
メールアドレス ユーザー名/Maildir/

> sudo vi /etc/postfix/vmailbox

info@mail.example.com info/Maildir/

postmap コマンドでマッピングファイルを作成

> sudo postmap /etc/postfix/vmailbox


PostfixDovecot再起動

sudo systemctl restart postfix
sudo systemctl restart dovecot




ポート開放 iptables の設定

メールを送受信するためにポートを開けます。
110番 = メール受信(IMAP)
143番 = メール受信(POP)
587番 = メール送信(SMTP)

> sudo vi /etc/iptables/iptables.rules

-A INPUT -p tcp --dport 110 -j ACCEPT
-A INPUT -p tcp --dport 143 -j ACCEPT
-A INPUT -p tcp --dport 587 -j ACCEPT

変更の反映

sudo iptables-restore < /etc/iptables/iptables.rules

現在のルールの確認

sudo iptables -L -n -v --line-numbers




メールクライアントの設定(Thunderbird バージョン52)

Thunderbirdを起動して、「ファイル」 => 「新規作成」 => 「既存のメールアカウント」
名前、メールアドレス、パスワードを入力
この時のパスワードは、SCRAM-SHA-1のパスワードの生成のときに入力したのを使う。

f:id:bytefunc:20170902144522p:plain

下記のようにサーバーからアカウント設定が見かればおkです。

f:id:bytefunc:20170902144532p:plain




参考サイト

(メモ) Ubuntu(16.0.4) crontab のエディタ変更方法

Ubuntu(16.0.4) での crontab のエディタ変更方法のメモ

下のコマンドを実行してエディタのセレクト画面を表示

sudo select-editor

1-3の数字の中からエディタを選択する

Select an editor.  To change later, run 'select-editor'.
  1. /bin/ed
  2. /bin/nano        <---- easiest
  3. /usr/bin/vim.tiny

Choose 1-3 [2]: 

Windowsでフォルダーが削除できない場合の対処

たまにWindowsのフォルダーが削除できなくなるのでコマンドプロンプトでの対処方法をメモ。

特にフォルダーの名前が複雑だったり長い場合に便利な方法。

コマンドプロンプト

dir /X

を入力して削除したいフォルダーのショートネームを表示。

ショートネームを"rd"コマンドに指定する。

rd *****

(メモ) Ubuntu(16.0.4) Nginx, MySQL の自動起動の設定

Ubuntu(16.0.4)で起動時にNginx MySQL自動起動設定する

// nginx 自動起動
sudo /lib/systemd/systemd-sysv-install enable nginx // 有効化
sudo /lib/systemd/systemd-sysv-install disable nginx // 無効化


// mysql 自動起動
sudo /lib/systemd/systemd-sysv-install enable mysql // 有効化
sudo /lib/systemd/systemd-sysv-install disable mysql // 無効化

Electron で jQuery やjQuery依存のライブラリを使用する方法

electronでjQueryjQuery依存のライブラリを使おうとしたら未定義のエラーが出ました

Uncaught ReferenceError: $ is not defined

Uncaught ReferenceError: jQuery is not defined

  

下記の読み込み方では未定義のエラーになる

<script src="./js/jquery.min.js"></script>

  

     

これでうまく動きました

<script>
    window.jQuery = window.$ = require('./js/jquery.min.js');
</script>

ElectronのgetAppPathとapp.getPathのパス出力

メモ

electron v1.4.1

// windows
console.log(app.getAppPath());         // 現在のアプリケーションディレクトリ

console.log(app.getPath('home'));      // C:\Users\ユーザー名
console.log(app.getPath('appData'));   // C:\Users\ユーザー名\AppData\Roaming
console.log(app.getPath('userData'));  // C:\Users\ユーザー名\AppData\Roaming\パッケージ名
console.log(app.getPath('temp'));      // C:\Users\ユーザー名\AppData\Local\Temp
console.log(app.getPath('exe'));       // 実行中の.exeパス 
console.log(app.getPath('module'));    // 実行中の.exeパス
console.log(app.getPath('desktop'));   // C:\Users\ユーザー名\Desktop
console.log(app.getPath('documents')); // C:\Users\ユーザー名\Documents
console.log(app.getPath('downloads')); // C:\Users\ユーザー名\Downloads
console.log(app.getPath('music'));     // C:\Users\ユーザー名\Music
console.log(app.getPath('pictures'));  // C:\Users\ユーザー名\Pictures
console.log(app.getPath('videos'));    // C:\Users\ユーザー名\Videos
console.log(app.getPath('pepperFlashSystemPlugin')); // エラー (Pepper Flash Pluginを有効化しないとエラー)