ES6 기능이 활성화 된 상태에서 Node.js 앱을 실행하는 방법은 무엇입니까?
es6features를 사용하여 노드 앱을 실행하려면 BabelJS (이전 이름 : 6to5 ) 의 require 후크 를 사용합니다 .
// run.js
require("babel/register");
require("./app.js6");
node run.js
내 app.js6 실행을 호출 합니다 . BabelJS를 설치하고 es6features 를 사용하려는 각 프로젝트에 대해 run.js 를 제공해야합니다 . 나는 같은 전화를 선호합니다 nodejs6 app.js6
. 이 시스템을 독립적으로 어떻게 구현할 수 있습니까 (Unix 및 Windows)?
앱의 package.json 파일에 babel-cli
및 babel-preset-es2015
(일명 ES6) 종속성을 추가하고 start
스크립트를 정의합니다 .
{
"dependencies": {
"babel-cli": "^6.0.0",
"babel-preset-es2015": "^6.0.0"
},
"scripts": {
"start": "babel-node --presets es2015 app.js"
}
}
그런 다음 다음 명령을 실행하여 앱을 실행할 수 있습니다.
npm start
Babel 사용을 중지하기로 결정한 경우 (예 : Node.js가 모든 ES6 기능을 지원하면) package.json에서 제거하면됩니다.
{
"dependencies": {},
"scripts": {
"start": "node app.js"
}
}
이것의 한 가지 이점은 앱을 실행하는 명령이 동일하게 유지되어 다른 개발자와 작업하는 경우 도움이된다는 것입니다.
es6 지원 및 파일 변경시 서버 다시로드를 사용하여 node.js 앱을 구성하는 방법 .
I. 구성 단계 (처음부터 프로젝트 생성) :
1. 터미널에서 프로젝트 기본 디렉토리로 이동하십시오.
npm init
// 프로젝트 용 package.json 생성
2. 의존성 설치
npm install --save-dev babel
npm install --save-dev babel-cli
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-stage-0 //*1
npm install --save-dev nodemon
1-1 단계 또는 2 단계 일 수도 있으며 사용하려는 es의 기능에 따라 다릅니다.
3. package.json 파일에 다음과 같은 내용이 있어야합니다 (패키지 버전은 다를 수 있지만 괜찮습니다).
"devDependencies": {
"babel": "^6.5.2",
"babel-cli": "^6.16.0",
"babel-preset-es2015": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"nodemon": "^1.11.0"
}
4. 루트 프로젝트 디렉토리에 .babelrc 파일을 생성합니다 (package.json 파일이 있습니다).
{
"presets": ["es2015", "stage-0"]
}
5. 두 개의 디렉터리를 만듭니다.
SRC - 여기가 ES6에 writen 파일과 디렉토리를하고있다
DIST - 여기 파일은 바벨을 사용 ES5하는 컴파일
프로젝트 루트 디렉토리는 다음과 같아야합니다.
- 계획
- src
- index.js // 기본 프로젝트 파일
- dist
- package.json
- .babelrc
- src
7. package.json에 필요한 명령을 추가합니다.
"scripts": {
"watch": "babel -w src/ -d dist/",
"build": "babel src/ -d dist/",
"serve": "babel -w src/ -d dist/ | nodemon --watch dist",
"test": "echo \"Error: no test specified\" && exit 1"
}
8. 사용 가능한 명령 :
npm run watch
// src 디렉토리에서 watch watch 변경을 시작하고 dist로 컴파일합니다.
npm run build
// src 디렉토리에서 dist로 파일을 컴파일합니다.
npm run serve
// 그것은 감시 + 시작 노드 서버를하고 있으며, 모든 파일 변경시 dist 디렉토리 변경을 감시하는 nodemon을 사용하여 노드 서버를 다시 시작합니다.
9. 최종 노트
- 서버는 dist / index.js 파일을 메인 파일로 실행합니다.
- dist / index.js 파일은 src / index.js에서 컴파일되므로 프로젝트의 메인 파일이 있어야합니다.
- dist 디렉토리는 git에 의해 무시되도록 추가되어야합니다 (하지만 노드 패키지 일 경우 npm에 대해서는 무시하지 마십시오)
10. 서버를 실행하고 src 디렉터리에 앱 생성을 시작합니다 .
npm run serve
II. Easier way ( ready to use boilerplate )
If it is too many points for You then full woking boilerplate is available on github - https://github.com/maciejsikora/node-express-babel-boilerplate.
You can use node with --harmony flag to run script with es6 features
you need to install babel-register
and babel-preset-es2015
preset Which used into babel-register
options to Enabled convert ES6
to ES5
on-the-fly transpilation
npm install babel-register
npm install babel-preset-es2015
your run.js file:
// require babel-register and set Babel presets options to es2015
require('babel-register')({
presets: [ 'es2015' ]
});
require("./app.js6");
Notice: Now you does not need .babelrc
file to set Babel presets
options As we setting it with require
method
node -r babel-register scripts.js
This is the best solution
npx babel-node scripts.js
!Babel node doesn't work well in case of exit process and kexec
package also doesn't help in this case (as I tried)
In both cases you need to use .babelrc
which should describe presets and plugins for your app.
npx
is using only for execution of libraries which are not installed with npm
or yarn
. Otherwise you need to npm i -g babel-cli
and then babel-node script.js
I would prefer a call like
nodejs6 app.js6
.
You may try the wrapper solution with babel-core api:
// Save as es6.js
var babel = require("babel-core");
var argc = process.argv.length;
babel.transformFile(process.argv[argc - 1], function (err, result) {
eval(result.code);
});
Run your es6 featured script with node es6 thefile.js
Reference: offical usage doc
As of babel 6, you now must install babel-register
and use the following
require("babel-register");
Be sure to also install the babel es2015 preset.
Refer this:
https://stackoverflow.com/a/51485027/1549191
or this boilerplate:
참고URL : https://stackoverflow.com/questions/28782656/how-to-run-node-js-app-with-es6-features-enabled
'Development Tip' 카테고리의 다른 글
Linux에서 Atom 텍스트 편집기를 제거하는 방법은 무엇입니까? (0) | 2020.11.09 |
---|---|
순수한 Swift 프로젝트에서 실행중인 단위 테스트를 앱에 알리는 방법은 무엇입니까? (0) | 2020.11.09 |
Angular2 IE11 정의되지 않은 참조 또는 null 참조의 'apply'속성을 가져올 수 없습니다. (0) | 2020.11.09 |
정규식으로 파일 유형 유효성 검사 (0) | 2020.11.09 |
이름-값 쌍의 의미 및 구조 (0) | 2020.11.08 |