This question already has answers here:
Wildcard or asterisk (*) vs named or selective import es6 javascript
(4 answers)
using brackets with javascript import syntax
(2 answers)
Import Statements in ES6 from MDN docs
(2 answers)
When should I use curly braces for ES6 import?
(11 answers)
Closed 3 years ago.
In your experience what's the best practice when importing large modules into your component. Could you please tell me which and the reason why from the example below?
import * from './foo'
or
import {bar, beer, brew } from './foo'
import * as name from './foo' will import everything from foo, but the second statement will only import three exports from the module.
It's very context specific, but in my opinion it's much easier to see what is actually imported in the second example, and you will also be able to take advantage of tree shaking if e.g. not all exports from a library are used.
Related
This question already has answers here:
"Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6
(31 answers)
Closed 6 months ago.
I encounter this error when I want to import a package:
SyntaxError: Cannot use import statement outside a module
import:
import inquirer from 'inquirer';
help me.
Try adding
"type": "module",
to you package.json file
This question already has answers here:
How to resolve Node.js ES6 (ESM) Modules with the TypeScript Compiler (TSC). TSC doesn't emit the correct file-ext
(2 answers)
Appending .js extension on relative import statements during Typescript compilation (ES6 modules)
(11 answers)
How to make TypeScript output valid ES6 module import statements?
(3 answers)
Closed 1 year ago.
If I import a file import * as test from './dir/file', the .js extension is missing in the build
I want the output file like this import * as test from './dir/file.js'
The problem is: YES I can add the .js manually BUT many libraries doesn't have the .js extension manually added, and I don't want to go through all files of a library and add the extension manually.
And I know there are some regex replacer out there which add the .js extension. But is this the only way?? Is there no way to tell TypeScript to add the .js (maybe via tsconfig.json)???
There is a github issue opened about this, check it out here for workarounds, as I don't think it is possible with current ts compiler.
This question already has answers here:
Pass options to ES6 module imports
(9 answers)
Closed 5 years ago.
In CommonJS one can run:
const module = require('module')(API_KEY)
In the various ways TC39 / 'ES6' modules (inverted commas as there are are more modules writted in ES6 using CommonJS than this syntax), how can I do the same?
It looks like this isn't possible, ie my only option is:
import moduleSetup from "module";
const module = moduleSetup(API_KEY);
ie my only option is
Correct, at least for now.
It may be that the dynamic import proposal will let you do something like the require call, but it's at Stage 3 right now. (Of course, if you use them for this, the import won't be statically-analyzable anymore...)
This question already has answers here:
using brackets with javascript import syntax
(2 answers)
Closed 5 years ago.
In one of the tutorials on ReactJS I just happened to see the following code.
import React from 'react';
import {createStore} from 'redux';
Question 1: So what's the point of {}, I'm sure something must be really interesting and meaningful.
Question 2: I come from Python background, so trying to see how similar/different the importing in ES6 is different from Pythonic way (we don't have {} types in python .
In this case, {} is a destructuring assignment. It has to do how redux exports createStore function.
comparing it to python it would be
from redux import createStore
vs
import react
This question already has answers here:
Using Node.js require vs. ES6 import/export
(11 answers)
Closed 6 years ago.
Right now I'm importing modules into node project as
import * as name from "moduleName";
Im not doing it as
var name = require("moduleName");
as we used to be in the node project, earlier
my question is there difference in the writing a module when we import using require or import, are modules written internally are same just we are importing in different way or its something internal that forces us to use require or import when importing
and what is the difference between require and import(es6)
Thanks!
import runs at the beginning of the file and it would be already
loaded before the code itself runs.
require on the other hand is run
inline and can be inserted within the code conditionally.