es6-modules

detect whether ES Module is run from command line in Node

三世轮回 提交于 2019-12-20 03:56:09
问题 When using CommonJS modules in Node, you can detect whether a script is being run from the command line using require.main === module . What is an equivalent way to detect whether a script is being run from the command line when using ES Modules in Node (with the --experimental-modules flag)? 回答1: There is none - yet (it's still experimental!). Although the prevailing opinion is that such a check is a bad practice anyway and you should just provide separate scripts for the library and the

Why does mutating a module update the reference if calling that module from another module, but not if calling from itself?

本小妞迷上赌 提交于 2019-12-20 03:48:15
问题 This question pertains to testing javascript and mocking functions. Say I have a module that looks like this: export function alpha(n) { return `${n}${beta(n)}${n}`; } export function beta(n) { return new Array(n).fill(0).map(() => ".").join(""); } Then I can't test it the following way: import * as indexModule from "./index"; //Not what we want to do, because we want to mock the functionality of beta describe("alpha, large test", () => { it("alpha(1) returns '1.1'", () => { expect

ES6: import specific values with namespace

廉价感情. 提交于 2019-12-20 01:53:09
问题 There is a specific thing i want to do from time to time, that i cannot figure out: suppose module1.js exports 3 values: //module1.js export const a = 'a'; export const b = 'b'; export const c = 'c'; And then, in module2.js , I want to import two of these, into an object (as a kind of namespace thing): //module2.js import { a, c } as constants from './module1'; //<-WRONG! So what I end up doing is something like this: //module2.js import { a, c } from './module1'; const constants = { a, c };

How to dynamically execute/eval JavaScript code that contains an ES6 module / requires some dependencies?

霸气de小男生 提交于 2019-12-19 07:10:07
问题 I want my users to be able to use JavaScript as a scripting language inside my JavaScript application. In order to do so, I need to dynamically execute the source code. There seem to be two main options for dynamically executing JavaScript: a) Use eval(...) method ( or var func = new Function(...); ) . b) Add a <script> node to the DOM (for example by using $('body').append(...) ). Both methods work fine as long as I do not use any import statements in the dynamically executed source code. If

ES6 javascript import external js file

浪子不回头ぞ 提交于 2019-12-19 05:08:32
问题 I'm trying this in my ES6 javascript module: import externalutil from 'https://externalutil.com/js/externalutil.js'; but it fails with this transpile error: Module not found: 'https://externalutil.com/js/externalutil.js' The file externalutil.js is an old-fashioned javascript library that does not export anything. Any help will be appreciated. 回答1: You cannot import URL's (as of yet). You should download the file, put it somewhere locally and reference it locally as well. However in your case

module specifier in es6 import and export

孤街浪徒 提交于 2019-12-18 05:47:05
问题 I'm confused about what the module specifier in these statements refer to: export {bar} from "foo"; import {bar} from "foo"; What does "foo" refer to? It can't be a file since it would be something like "./foo" . If it's not a file, I assume it's an ID. How is the ID defined? I'm exporting from a js file, but the import would be part of an inline html script ( type="module" ) in the firefox browser. The browser version (and browser settings) have been verified to work with es6 modules. Thanks

Javascript reference vs binding…what's the difference?

旧时模样 提交于 2019-12-18 04:41:37
问题 I recently read the following in Kyle Simpson's You Don't Know JS: ES6 "[ES6 modules export] actual bindings (almost like pointers) to the identifiers in your inner module definition." My confusion is how these bindings differ from references... I understand that a reference in JS is only applicable to non-primitive types (like objects), so that given let object1 = {a: 1}; let object2 = object1; object1 and object2 now refer to (they are both references to) the same object. If I add a

Javascript reference vs binding…what's the difference?

若如初见. 提交于 2019-12-18 04:41:23
问题 I recently read the following in Kyle Simpson's You Don't Know JS: ES6 "[ES6 modules export] actual bindings (almost like pointers) to the identifiers in your inner module definition." My confusion is how these bindings differ from references... I understand that a reference in JS is only applicable to non-primitive types (like objects), so that given let object1 = {a: 1}; let object2 = object1; object1 and object2 now refer to (they are both references to) the same object. If I add a

Does ES6 import/export need “.js” extension?

亡梦爱人 提交于 2019-12-17 20:19:09
问题 I installed chrome beta - Version 60.0.3112.24 (Official Build) beta (64-bit) In chrome://flags/ I enabled 'Experimental Web Platform features' (see https://jakearchibald.com/2017/es-modules-in-browsers) I then tried: <script type="module" src='bla/src/index.js'></script> where index.js has a line like: export { default as drawImage } from './drawImage'; This refer to an existing file drawImage.js what I get in the console is error in GET http://localhost/bla/src/drawImage If I change the

Chrome 61: Unexpected token import

折月煮酒 提交于 2019-12-17 18:37:10
问题 Running Chrome 61 which is supposed to support module loading with import . Indeed Paul's demo works for me. However, when I try it myself I get a JS error "Unexpected token import". Chrome seems to balk at import : test.html <!doctype html> <html> <body> <script src="test.js"></script> </body> </html> test.js: import {hello} from './something.js' console.log(hello()) something.js export {hello} function hello() { return "hello world" } Why does Chrome not understand "import" 回答1: For those