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';
Related
This question already has answers here:
Pass options to ES6 module imports
(9 answers)
Closed 4 months ago.
I want to import modules in a single line with ES6 modules, like it was done with the require syntax shown in the example.
var configApi = require('somemodule').config.get('services').api;
I've tried
import configApi from 'somemodule'.config.get('services').api;
and
import configApi from 'somemodule';
const api = confiApi.config.get('services').api;
but none of them have work.
I think what you are looking for is:
import * as configApi from "thethingsio-domain";
const api = confiApi.config.get('services').api;
but make sure you look at what is being exported by thethingsio-domain
Did you try using JavaScript dynamic importants.
import("/modules/my-module.js")
.then((module) => {
// do something
})
.catch((err) => {
console.error(err)
});
For more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
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/
This question already has answers here:
module.exports vs exports in Node.js
(24 answers)
Closed 6 years ago.
I've been sitting here for 3 hours straight, trying to figure out how I could possibly export a class from one file, require it (multiple times) in other files, so that that class can be extended.
My current code looks something like this:
// base.js //
class Base {
constructor() { ... }
}
exports = Base;
// extension.js //
var Base = require('./base.js');
class Extension extends Base {
constructor() { ... }
}
I get an Exception telling me that 'Base' is not a constructor. When I run console.log(Base), I get an empty object. Am I doing something completely wrong, or is my desire not possible?
I'd be glad for any help given!
Node doesn't support normal ES6 exporting yet. So module.exports is required as said in the comments.
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.
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