es6-modules

Alternative for __dirname in node when using the --experimental-modules flag

浪子不回头ぞ 提交于 2019-11-27 01:28:52
问题 I use the flag --experimental-modules when running my node application in order to use ES6 modules. However when I use this flag the metavariable __dirname is not available. Is there an alternative way to get the same string that is stored in __dirname that is compatible with this mode? 回答1: As of Node.js 10.12 there's an alternative that doesn't require creating multiple files and handles special characters in filenames across platforms: import { dirname } from 'path'; import { fileURLToPath

ES2015 “import” not working in node v6.0.0 with with --harmony_modules option

荒凉一梦 提交于 2019-11-27 00:43:43
I am using node v6.0.0 and wanted to use ES2016 (ES6). However I realized that the "import" syntax is not working. Isn't "import" fundamental to for writing modular code in ES2015? I tried running node with --harmony_modules option as well but still got the same error about "import". Here's the code. Working code without "import": '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); Notworking code with "import": server.js 'use strict'; import

Inlining ECMAScript Modules in HTML

為{幸葍}努か 提交于 2019-11-27 00:20:31
问题 I've been experimenting with new native ECMAScript module support that has recently been added to browsers. It's pleasant to finally be able import scripts directly and cleanly from JavaScript. /example.html 🔍 <script type="module"> import {example} from '/example.js'; example(); </script> /example.js export function example() { document.body.appendChild(document.createTextNode("hello")); }; However, this only allows me to import modules that are defined by separate external JavaScript files.

How can I alias a default import in Javascript?

十年热恋 提交于 2019-11-26 23:57:10
问题 Using ES6 modules, I know I can alias a named import import { foo as bar } from 'my-module'; And I know I can import a default import import defaultMember from 'my-module'; I'd like to alias a default import and I had thought the following would work import defaultMember as alias from 'my-module'; but that results in a parsing (syntax) error. How can I (or can I?) alias a default import? 回答1: defaultMember already is an alias - it doesn't need to be the name of the exported function/thing.

Not recommended to use “use strict” in ES6?

别说谁变了你拦得住时间么 提交于 2019-11-26 23:33:34
I'm not familiar with ECMAScript 6 yet. I've just cloned the React Starter Kit repo, which uses ES6 for application code. I was surprised to see that the linter is configured to forbid occurences of the use strict directive, which I thought was recommended in pre-ES6 JavaScript. So what's the point? ES6 modules are always in strict mode. To quote the relevant part of the spec : 10.2.1 Strict Mode Code An ECMAScript Script syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. Code is interpreted as strict mode code in the following situations: Global

What is the difference between importing a function expression or a function declaration from a ES6 module?

自作多情 提交于 2019-11-26 20:28:23
问题 As I understand it (see section 16.3.2.1), ES6 allows different syntaxes for function / class export operands. The difference refers to whether the exported function needs to be interpreted at import as a function declaration, in which case you write: export default function () {} // (a) or as a function expression: export default (function () {}); // (b) . As a possible related sidenote: I read that imports are hoisted, but I'm not really sure what that means in this context. Taking the case

How will browsers handle ES6 import/export syntax

半世苍凉 提交于 2019-11-26 17:49:09
问题 I've been thinking around this question lot of days and i have decided to ask the experts. How browsers will handle the new import/export syntax ? I mean: will the modules be loaded asynchronously ? Referencing only my main or entry file and browsers will lazy load the requiere modules. Maybe am i missing or misunderstanding something about this new architecture ? Thank you very much! Regards. 回答1: This is standardized now and supported by all major modern browsers. will the modules be loaded

What is difference between Axios and Fetch?

孤街醉人 提交于 2019-11-26 17:21:25
I am calling the web service by using fetch but the same I can do with the help of axios. So now I am confused. Should I go for either axios or fetch? They are HTTP request libraries... I end up with the same doubt but the table in this post makes me go with isomorphic-fetch . Which is fetch but works with NodeJS. http://andrewhfarmer.com/ajax-libraries/ Fetch and Axios are very similar in functionality, but for more backwards compatibility Axios seems to work better (fetch doesn't work in IE 11 for example, check this post ) Also, if you work with JSON requests, the following are some

ES6 Destructuring and Module imports

北城余情 提交于 2019-11-26 13:47:32
问题 I was under the impression that this syntax: import Router from 'react-router'; var {Link} = Router; has the same final result as this: import {Link} from 'react-router'; Can someone explain what the difference is? (I originally thought it was a React Router Bug.) 回答1: import {Link} from 'react-router'; imports a named export from react-router , i.e. something like export const Link = 42; import Router from 'react-router'; const {Link} = Router; pulls out the property Link from the default

Which browsers support import and export syntax for ECMAScript 6?

£可爱£侵袭症+ 提交于 2019-11-26 13:37:46
I am currently writing a web application using the MEAN Stack, and am attempting to write code in ECMAScript 6 JavaScript; however, I am getting errors in both Chrome and Firefox when using import and export syntax. Are there currently any browsers that fully support ECMAScript 6? Please note: I am not asking when ECMAScript 6 will be supported by browsers. I'm asking which browsers support ECMAScript 6 import and export syntax. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla#Features_not_yet_supported_by_Firefox Chrome and Firefox