Webpack 을 이용하여 번들링시에 package.json 에 기술된 모듈들은 대부분 번들링프로세싱에서 제외시키고 번들링을 한다.

{
    test: /\.(js|jsx)$/,
    // package.json 에 기술되어 설치된 npm 모듈들은 Babel 에서 제외 "/node_modules/" 하위 디렉토리
    exclude: /node_modules/,
    loader: 'babel-loader',
    options: {
      presets: ['@babel/preset-env']
    }
},

하지만 최근 일부 npm 모듈들은 IE11 를 지원하지 않는 모듈들도 나타나서 이런 모듈들을 포함해서 쓸경우 IE에서 실행할경우 ES6 미지원에 대한 에러가 발생하는 경우가 있다.

이런 브라우져들도 지원을 하려면 이때 package.json 에 기술되었더라도 Babel 을 이용한 번들링이 필요한데, 이때 다음과 같은 정규식 문법으로 exclude 필드에서 제외하는 방법이 있다.

{
    test: /\.(js|jsx)$/,
    // swagger-parser - v10.0.2
    // swagger-parser 의 경우 내부적으로 ES6의 모듈 2개에 대한 디펜던시를 가지고 있는데
    // 해당 모듈들을 찾아서 다음과같이 babel-loader 에 exclude 에 포함되지 않도록 설정한다
    exclude: /node_modules\/(?!(@apidevtools|@jsdevtools))/,
    loader: 'babel-loader?cacheDirectory',
    options: {
      presets: ['@babel/preset-env']
    }
},

위와같이 처리하고 Webpack 번들링을 수행하면, IE11 에서도 에러없이 ES6가 폴리필처리되어 잘 수행된다.

참고: tech.kakao.com/2020/12/01/frontend-growth-02/

Posted by leechwin
,

특정 버전의 npm 을 설치할때에는 다음과 같이 실행한다.

  • npm i -g npm@{version}

예졔

  • $ npm i -g npm@6.4.1


Posted by leechwin
,

winston에서는 로그 파일 관리를 위해 다음과 같은 설정을 제공한다.

  • File Transport Options
    • maxsize: Max size in bytes of the logfile, if the size is exceeded then a new file is created, a counter will become a suffix of the log file.
      • 로그파일의 최대 크기를 지정가능하고, 해당 크기를 넘을 경우 새로운 로그파일을 생성
        • ex) error.log, error1.log, error2.log
    • maxFiles: Limit the number of files created when the size of the logfile is exceeded.
      • 생성되는 로그 파일의 갯수를 지정가능하고, maxFiles 갯수를 넘어갈경우 가장 오래된 파일을 삭제하고 새로 생성
    • zippedArchive: If true, all log files but the current one will be zipped.
      • 로그파일을 zip 파일로 유지
  • 예제
    • 로그파일을 최대 100MB x 5 개로 유지
    • var fileAppender = new winston.transports.File({
          timestamp: _timestamp,
          formatter: _customFormatter,
          json: false,
          filename: 'filelog.log',
          maxsize: 104857600, // 100 MB = 1024 * 1024 * 100 = 104857600 Bytes
          maxFiles: 5
      });
      
  • winston-daily-rotate-file
    • Daily로 로그를 생성하고 싶을 경우 winston-daily-rotate-file 모듈을 사용하면 된다.


Posted by leechwin
,

Winston의 Log Format을 Customizing 하는 방법에 대해 알아본다.


Winston Log Format을 수정하기 위해서는 Winston Transport의 formatter 설정을 구현해야 한다.

  • timestamp 및 output format 함수를 재구현
function _timestamp() {
        return moment().format("YYYY-MM-DD HH:mm:ss.SSS");
}
function _customFormatter(options) {
    return options.timestamp() +
            ' ['+ options.level.toUpperCase() + ']' +
            '['+ options.meta.loggerName + '] ' +
            (!options.meta.clientMessage ? options.message : options.meta.clientMessage);
}
var consoleAppender = new winston.transports.Console({
    timestamp: _timestamp,
    formatter: _customFormatter
});
var fileAppender = new winston.transports.File({
    timestamp: _timestamp,
    formatter: _customFormatter,
    json: false,
    filename: 'filelog.log'
});


Posted by leechwin
,

nodejs 설치후 $ node app.js 등과 같이 app을 실행시킬때 다음과 같은 에러가 발생하는 경우가 있다.

  •  /usr/bin/env: node: No such file or directory

원인은 node 명령이 등록이 되지않아서 인데, 실제로 nodejs 라는 명령으로 실행하면 실행이 된다.


이때는 다음과 같이 nodejs의 심볼릭 링크를 만들어 주면 된다.

  • $ ln -s /usr/bin/nodejs /usr/local/bin/node

이후 $ node app.js 명령이 실행이 된다.


참고: https://github.com/nodejs/node-v0.x-archive/issues/3911

'Node.js' 카테고리의 다른 글

[Node.js][Winston] Log File Managment  (0) 2017.02.03
[Node.js][Winston] Custom Log Format  (0) 2017.02.03
[Node.js] NVM을 통한 nodejs 설치  (0) 2016.12.21
[BOWER] Proxy 설정  (0) 2016.12.16
[NPM] Proxy 설정  (1) 2016.12.16
Posted by leechwin
,

NVM(Node Version Manager)를 통한 nodejs 설치에 대해 알아보자.


NVM을 통하여 기존 설치된 node.js 버전을 변경하는 동작도 가능하다.


다음 명령을 통해 NVM을 설치한다.

  • $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

설치된 NVM 버전은 다음 명령을 통해 확인 가능하다.

  • $ nvm --version
  • 0.32.1

NVM 설치가 잘 안되면 아래 명령으로 개발에 필요한 기본 패키지를 설치한다.

  • $ apt-get install build-essential libssl-dev


NVM 설치가 끝나면 다음과 같이 원하는 버전의 nodejs를 설치한다.

  • $ nvm install v12.18.2

$ nvm install v12.18.2

Downloading and installing node v12.18.2...

Downloading https://nodejs.org/dist/v12.18.2/node-v12.18.2-linux-x64.tar.xz...

######################################################################## 100.0%

Computing checksum with sha256sum

Checksums matched!

Now using node v12.18.2 (npm v6.14.5)


NVM을 통해 nodejs가 설치되면서 npm도 같이 설치가 된다.

  • $ npm -v
  • 3.10.8

현재 설치된 nodejs 버전은 다음 명령으로 확인가능하다.

  • $ nvm ls

$ nvm ls

       v10.17.0

->     v12.18.2

default -> v10.17.0

node -> stable (-> v12.18.2) (default)

stable -> 12.18 (-> v12.18.2) (default)

iojs -> N/A (default)

lts/* -> lts/erbium (-> v12.18.2)

lts/argon -> v4.9.1 (-> N/A)

lts/boron -> v6.17.1 (-> N/A)

lts/carbon -> v8.17.0 (-> N/A)

lts/dubnium -> v10.21.0 (-> N/A)

lts/erbium -> v12.18.2

 


현재 사용중인 nodejs 버전은 다음 명령으로 확인가능하다.

  • $ nvm current
  • v12.18.2
기본 nodejs 버전을 설정하려면 다음 명령으로 변경가능하다.
  • $ nvm alias default v12.18.2
  • default -> v12.18.2


Posted by leechwin
,

[BOWER] Proxy 설정

Node.js 2016. 12. 16. 11:01

bower install 실행시에 Proxy 환경일 경우 제대로 설치가 안되는 경우가 있다.


이때에는 bower.json 파일이나 .bowerrc 파일에 Proxy 설정이 필요하다.


예제

  • {
      "proxy": "http://yourProxy:yourPort",
      "https-proxy": "http://yourProxy:yourPort",
      "strict-ssl": false,
      "directory": "bower_components",
      "registry":"http://bower.herokuapp.com"
    }
만약 bower install 명력이 동작안하는 경우 bower.json 파일을 .bowerrc 로 변경해서 bower install 을 수행하여 본다.

bower.json 은 최신 bower버전에서 도입되어 구버전의 bower일 경우 인식안되는 경우가 있다.


Posted by leechwin
,

[NPM] Proxy 설정

Node.js 2016. 12. 16. 10:59

npm install 실행시에 Proxy 환경일 경우 제대로 설치가 안되는 경우가 있다.


이때에는 다음과 같이 npm config 명령을 통해 Proxy 설정이 필요하다.


예제

  • npm config set proxy http://111.111.111.111:8081
  • npm config set https-proxy https://111.111.111.111:8081
  • npm config set strict-ssl false
  • npm config set registry http://registry.npmjs.org/

npm 설정값은 다음 명령으로 확인가능하다.

  • npm config list
설정된 값은 ~/.npmrc 파일에 저장되어 있다.


Posted by leechwin
,

Windows 에서 node 관련 npm 명령등을 수행시에 다음과 같이 'node-gyp' 관련 에러가 발생 하는 경우가 있다.

  • npm ERR! time@0.11.4 install: `node-gyp rebuild` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the time@0.11.4 install script 'node-gyp rebuild'. npm ERR! This is most likely a problem with the time package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! node-gyp rebuild npm ERR! You can get their info via: npm ERR! npm owner ls time npm ERR! There is likely additional logging output above.


이것은 python 관련 환경 문제로 python 2.7과 Microsoft Visual Studio C++ 2012 Express를 설치해야한다.

설치 완료 후 다시 npm 명령을 수행하면 잘 실행이 된다.


Reference


Posted by leechwin
,

Heroku 에 Deploy 시킨 App 들이 잘 동작하다가 30분정도 지나면 잘 끊기는 현상이 발생한다.

원인은 Heroku 내부에서 각 App 마다 동작하는 엔진인 dyno 가 30분이 지나면 sleep모드로 진입하여서 발생하는 현상이다.


Dyno 의 Sleep을 방지하려면 30분이전에 deploy 된 App URL 을 주기적으로 연결하여, sleep 모드에 돌입하지 않게 하는 여러 방법이 있다.

  • hubot-heroku-keepalive
    • Hubot 에서 사용가능한 모듈로 되어 있어서 config 설정시 HUBOT_HEROKU_KEEPALIVE_URL 을 넣어 주면 된다.
  • Kaffeine
    • Heroku Deploy URL을 넣어주면 해당 사이트에서 주기적으로 URL에 연결을 하여, sleep 모드에 돌입하지 않게 해주는 프로젝트

Reference: https://quickleft.com/blog/6-easy-ways-to-prevent-your-heroku-node-app-from-sleeping/

Posted by leechwin
,