Using Node.js require vs. ES6 import/export

Update Since Node v12 (April 2019), support for ES modules is enabled by default, and since Node v15 (October 2020) it’s stable (see here). Files including node modules must either end in .mjs or the nearest package.json file must contain “type”: “module”. The Node documentation has a ton more information, also about interop between CommonJS and ES modules. Performance-wise there is always the … Read more

forEach is not a function error with JavaScript array

First option: invoke forEach indirectly The parent.children is an Array like object. Use the following solution: The parent.children is NodeList type, which is an Array like object because: It contains the length property, which indicates the number of nodes Each node is a property value with numeric name, starting from 0: {0: NodeObject, 1: NodeObject, length: 2, …} See more details in this article. Second … Read more

Node.js – SyntaxError: Unexpected token import

Node 13+ Since Node 13, you can use either the .mjs extension, or set {“type”: “module”} in your package.json. You don’t need to use the –experimental-modules flag. Modules is now marked as stable in node.js Node 12 Since Node 12, you can use either the .mjs extension, or set “type”: “module” in your package.json. And you need to run node with the –experimental-modules flag. Node 9 In Node 9, it is enabled behind a flag, and … Read more

SyntaxError: Unexpected token import – Express

NodeJS supports import natively only experimentally, and only if your script has the .mjs extension. That’s why the start in your package.json is referring to babel-node, which transpiles ES6 code into classic JS on-the-fly before running it. But I doubt even that command will work, because you’re not passing any presets to babel to run the script. Try this command: nodemon … Read more

Node.js – SyntaxError: Unexpected token import

Node 13+ Since Node 13, you can use either the .mjs extension, or set {“type”: “module”} in your package.json. You don’t need to use the –experimental-modules flag. Modules is now marked as stable in node.js Node 12 Since Node 12, you can use either the .mjs extension, or set “type”: “module” in your package.json. And you need to run node with the –experimental-modules flag. Node 9 In Node 9, it is enabled behind a flag, and … Read more

How to import jquery using ES6 syntax?

index.js First, as @nem suggested in comment, the import should be done from node_modules/: Well, importing from dist/ doesn’t make sense since that is your distribution folder with production ready app. Building your app should take what’s inside node_modules/ and add it to the dist/ folder, jQuery included. Next, the glob –* as– is wrong as I know what object I’m importing (e.g. jQuery and $), so … Read more