code

--harmony_modules 옵션을 사용하여 노드 v6.0.0에서 ES2015 "가져 오기"가 작동하지 않음

codestyles 2020. 10. 7. 07:39
반응형

--harmony_modules 옵션을 사용하여 노드 v6.0.0에서 ES2015 "가져 오기"가 작동하지 않음


노드 v6.0.0을 사용하고 있으며 ES2016 (ES6)을 사용하고 싶었습니다. 그러나 "import"구문이 작동하지 않는다는 것을 깨달았습니다. ES2015에서 모듈 코드를 작성하기 위해 "가져 오기"가 기본이 아닙니까? --harmony_modules옵션으로 노드를 실행하려고 했지만 여전히 "가져 오기"에 대해 동일한 오류가 발생했습니다. 여기에 코드가 있습니다.

"가져 오기"가없는 작업 코드 :

'use strict';
let sum = 0;
class Number {

  addNumber(num1, num2) {
    return num1 + num2;
  }
}
let numberObj = new Number();
sum = numberObj.addNumber(1,2);
console.log("sum of two number 1 and 2 "+ sum);

"가져 오기"로 작동하지 않는 코드 :

server.js

'use strict';
import Number from "./Number";

let sum = 0;


let numberObj = new Number();

sum = numberObj.addNumber(1,2);
console.log("sum of two number 1 and 2 "+ sum);

Number.js

'use strict';
export default class Number {

  addNumber(num1, num2) {
    return num1 + num2;
  }
}

또한 http://node.green/ 을 확인하여 지원되는 es6를 확인했지만 --harmony_modules 옵션과 함께 작동하지 않는 이유를 이해할 수 없습니다. 도와주세요.


아직 구현되지 않았습니다.

Node 6.0.0은 대부분의 ES6 기능이 완료된 V8 버전을 사용합니다. 불행히도 모듈은 완성 된 기능 중 하나가 아닙니다.

node --v8-options | grep harmony 

진행중인 조화 플래그가 완전히 구현되지 않았으며 일반적으로 작동하지 않습니다.

--es_staging (테스트 할만한 하모니 기능 활성화 ( 내부 전용 ))
--harmony (완성 된 모든 하모니 기능
활성화 ) --harmony_shipping (제공된 모든 하모니 기능
활성화 ) --harmony_object_observe ( "harmony Object.observe"활성화 ( in progress ))
--harmony_modules ( "harmony 모듈"활성화 ( 진행 중 ))
--harmony_function_sent ( "harmony function.sent"활성화 ( 진행 중 ))
--harmony_sharedarraybuffer ( "harmony sharedarraybuffer"활성화 ( 진행 중 ))
--harmony_simd ( "harmony simd"활성화 ( 진행 중 ))
--harmony_do_expressions ( "harmony do-expressions" 활성화 ( 진행 중 ))
--harmony_iterator_close ( "harmony 반복기 종료"활성화 ( 진행 중 ))
--harmony_tailcalls ( "하모니 테일 호출"활성화 ( 진행 중 ))
--harmony_object_values_entries ( "harmony Object.values ​​/ Object.entries" 활성화 ( 진행 중 ))
--harmony_object_own_property_descriptors ( "harmony Object.getOwnPropertyDescriptors ()" 활성화 ( 진행 중 ))
--harmony_regexp_property ( "조화 유니 코드 정규 표현식 속성 클래스"활성화 ( 진행 중 )) )
--harmony_function_name ( "하모니 함수 이름 추론 ")
--harmony_regexp_lookbehind ( "harmony regexp lookbehind"
활성화 ) --harmony_species ( "harmony Symbol.species"
활성화 ) --harmony_instanceof ( "harmony instanceof support"
활성화 ) --harmony_default_parameters ( "harmony default parameters"
활성화 ) --harmony_destructuring_assignment (활성화 "harmony destructuring assignment")
--harmony_destructuring_bind ( "harmony destructuring bind"
활성화 ) --harmony_tostring ( "harmony toString"
활성화 ) --harmony_regexps ( "harmony 정규 표현식 확장"
활성화 ) --harmony_unicode_regexps ( "harmony unicode regexps"활성화)
--harmony_sloppy ( "조잡한 모드에서 조화 기능 활성화")
--harmony_sloppy_let ( "조잡한 모드에서 하모니 렛"활성화)
--harmony_sloppy_function ( "harmony sloppy function block scoping"
활성화 ) --harmony_proxies ( "harmony proxies"
활성화 ) --harmony_reflect ( "harmony Reflect API"
활성화 ) --harmony_regexp_subclass ( "harmony regexp 서브 클래 싱 활성화")


@Paulpro의 답변에 대한 댓글이어야하지만 댓글을 게시 할 충분한 담당자가 없습니다.

들어 윈도우 사용자에 해당하는 명령은 다음과 같습니다

node --v8-options | findstr harmony

모듈이 구현 될 때까지 Babel "트랜스 파일러" 를 사용하여 코드를 실행할 수 있습니다.

npm install --save babel-cli babel-preset-node6
./node_modules/.bin/babel-node --presets node6 ./your_script.js

참조 https://www.npmjs.com/package/babel-preset-node6https://babeljs.io/docs/usage/cli/

Downsides: this has various downsides, such as extra compilation time, which can be significant and you now need source maps for debugging; just saying.


As is stated above, ES6 modules are not implemented yet.

It appears to be a non-trivial issue to implement ES6 modules in a way that would be backward-compatible with Common JS modules, which is the current Node.js module syntax.

However, there is a draft of an implementation, that introduces a new file extension - .mjs - for a files containing ES6 modules.

Also, there is a counter-proposal that present an alternative approach of declaring all files with ES6 modules in package.json like so:

{
    "modules.root": "/path/to/es6/modules"
}

참고URL : https://stackoverflow.com/questions/36901147/es2015-import-not-working-in-node-v6-0-0-with-with-harmony-modules-option

반응형