import and call a function with es6 [duplicate] - javascript

This question already has answers here:
Pass options to ES6 module imports
(9 answers)
Closed 7 years ago.
Previously:
var debug = require('debug')('http')
, http = require('http')
, name = 'My App';
With es6, how can I import and invoke right away like the first line?
import debug from 'debug'();
is a no no?

You'll need two lines:
import debugModule from 'debug';
const debug = debugModule('http');
The import syntax is a declarative import syntax, it does not execute any functions.

is a no no?
Correct. Keep in mind that the import statement is analogous to more than a simple require() statement -- it also creates a binding of the "loaded" module to a local variable.
That is,
import debug from 'debug'();
...is more close in behavior/semantics to
var debug = require('debug');
...than it is to simply
require('debug');
Analogies to commonjs-style module loaders will obviously break down at some point, but at the end of the day it's a "no no" due to the plain and simple fact that import debug from 'debug' doesn't actually resolve to anything that you can invoke (or otherwise reference).

import http from "debug"; // not sure if this is the desired effect

Related

How is this if block being executed in this imported file? [duplicate]

This question already has answers here:
es6 import for side effects meaning
(3 answers)
Closed 12 months ago.
I have an index.js file (entry point in my react app) that imports 2 named exports from another file like so:
import { funcA, funcB } from './myFuncFile
and in myFuncFile I have an if statement at the top like so:
if (process.env.NODE_ENV === "local") {
// do some stuff
}
this might be a silly question but how is that if statement being called?
I assumed I'd have to import the whole file like:
import * as myFun from './myFuncFile
to get the if block to work? is the mere fact I'm just importing it, causing the if block to execute?
The simple answer is yes. When the file is imported as a whole, then this line will be triggered when the exported statements are loaded into memory.

How to use import with existing Node app? [duplicate]

This question already has answers here:
Node.js - SyntaxError: Unexpected token import
(16 answers)
Closed last year.
With Node 16.13.2 I am trying to add the validate module in an existing code base. Reading the 2 year old question I can't make it work with the below PoC. I get
import Schema from 'validate';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Question
Can anyone show me how the below PoC should look like for it to work?
index.js
const mod = require('./mod');
mod.js
import Schema from 'validate';
const test;
module.exports = test;
If you want to use the import syntax of es6+ then you will either need to use .mjs files (instead of regular .js files), or you will need to add in a compilation/transpilation step into your pipeline.
Using .mjs
If you change the file name of your mod.js file to mod.mjs, then this should work:
import Schema form 'validate';
export const test;
Then in index.js you will either have to change index.js to index.mjs and change the contents to:
import { test } from './mod.mjs';
..or you can keep index.js and change the contents to:
(async () {
const { test } = await import('./mod.mjs')
})();
You can read more in this rather comprehensive article i happened across while googling: https://blog.logrocket.com/how-to-use-ecmascript-modules-with-node-js/
Adding a compilation step
There are many different compilers and/or bundlers to pick from, but for regular vanilla javascript I'd recommend sticking to babel.
Freecodecamp has a tutorial for how to set up babel for use with nodejs: https://www.freecodecamp.org/news/setup-babel-in-nodejs/

Node.js module.export vs export caching

After going through node docs, I understand caching a little bit. Using
module.exports{constant_1, constant_2} will cache its assigned value, so everytime when we call require('') it will get the cached value instead of creating a new one.
example_1.js
const test = 'testValue';
module.export = {test};
example_2.js
const cachedValue = require('../example_1.js');
console.log(cachedValue);
But does the caching happen when we use the below syntax?
export {constant_1, constant_2} statement as well?
example_1.js
const test = 'testValue';
export {test};
example_2.js
import {test} from "example_1";
console.log(test);
import {test} from "example_1";
should be
import {test} from "./example_1.js";
like the CommonJS example, but otherwise the caching behavior is similar for ES modules. The node docs for ESM files for instance states
ES modules are resolved and cached as URLs. This means that files containing special characters such as # and ? need to be escaped.
So as long as the an ./example_1.js and another one somewhere else resolve to the same URL, it will be cached as the same module.

ES6, export all imports [duplicate]

This question already has an answer here:
ES6 Modules: transitively exporting symbols (i.e., from imported files)
(1 answer)
Closed 7 years ago.
I am trying to create a central file to coagulate all of my files. I am not sure if this is the correct way of doing this within es6, or even the right approach but my idea is like so :
import files1.file from '../file1/index';
import files2.file from '../file2/index';
import files3.file from '../file3/index';
export {* as files};
This does not seem to work, I'm wondering if there is some way of doing this or equivalent.
Did you try something like this? Or did you want to avoid it?
import files1.file from '../file1/index';
import files2.file from '../file2/index';
import files3.file from '../file3/index';
let files = { file1: files1.file,
file2: files2.file,
file3: files3.file };
export default files;
I am not exactly sure this is going to work but that's what I would try.

How would I translate this require to ES6 import style [duplicate]

This question already has answers here:
Pass options to ES6 module imports
(9 answers)
Closed 7 years ago.
I would like to do this
var debug = require('debug')('myapp');
... in ES6 without creating an extra variable. Can it be done?
import Debug from 'debug';
const debug = Debug('myapp');
(as lemieuxster said... addressing the fact that it is still listed under unanswered questions)
Note as mentioned in the comments, this will work for modules exported with the es6 syntax, that is whenever export default expression was used, which would give way to a require of the form var debug = require('./debug').default('myapp');. If the module you are importing used an export syntax of the type export const Debug = expression or export {Debug} or module.exports = {Debug : expression} then you will have to use import {Debug} from 'debug';

Categories

Resources