Require to Import with initialization paramater - javascript

Sorry if this is a repost/duplicate.
I'm needing some guidance with converting this require statement to an import statement.
I'm using the mailchimp API which has a require statement documented.
const mailchimpClient = require("mailchimp_transactional")("YOUR_API_KEY");
I need to convert this to an import statement as I'm using NestJS and eslint doesn't like require.
Thanks in advance.

import mailchamp from 'mailchimp_transactional'
const mailchimpClient = mailchimp("YOUR_API_KEY")
or
import * as mailchamp from 'mailchimp_transactional'
const mailchimpClient = mailchimp("YOUR_API_KEY")
depending on how your tsconfig looks like.

Related

How to Convert, in JS, an import statement to a require() statement

I have a Statement in JavaScript:
import app, { set } from '../app';
How can I write an equivalent statement in a form, such as I don't get an error like 'Cannot use import statement outside a module'.
I could convert similar less complex statement by using a format:
var Foo = require(foo);
but don't understand how to convert this one.
Not every import can be converted into a require. In pure Node.js this is only possible if the module being required is a CommonJS module, but the situation is different with tools such as Webpack or Babel.
I image you are using one of those tools, otherwise you would not be able to import ../app without a file extension in the first place: you would need to import from ../app.js or ../app/index.js explicitly, or to use export mappings, but that's a different story.
Then if
import app, { set } from '../app';
is a valid import in a ES module, with whatever loader, the equivalent CommonJS syntax would be
const app = require('../app');
const { set } = app;

How to dynamically import moment.js in a react app (with typescript)

I'm trying to do some code splitting and moment.js is one of the targeted packages I want to isolate in a separate chunk. I'm doing it like this:
const myFn = async (currentNb) => {
const moment = (await import('moment')).default()
const dateUnix: number = moment(currentNb).valueOf()
[...]
}
But I get the following error: this expression is not callable. Type Moment has no call signatures . So I guess I should be doing something like moment.valueOf(currentNb) but I'm not really sure. Can someone put some light to this? Thanks in advance
So apparently my syntax was incorrect. I finally found how to solve this problem. I need to import it as follows:
const {default: moment} = await import('moment')
I found the answer in the webpack documentation:
The reason we need default is that since webpack 4, when importing a
CommonJS module, the import will no longer resolve to the value of
module.exports, it will instead create an artificial namespace object
for the CommonJS module.

What is meaning of this command? import DataTypes = require('./lib/data-types');

import DataTypes = require('./lib/data-types')
Please someone explain to me. Why can use import and require together. This is in a module of NodeJS. Sequelize
This syntax is specific to TypeScript. See: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.
When exporting a module using export =, TypeScript-specific import module = require("module") must be used to import the module.
This is a Typescript syntax.
Try using
import * as x from 'x'
Or
const … = require(…)

Using Import * from single export

I am not used to seeing a import * often
The react + readux code I am going through uses import *
According to firefox documentation about import *
This inserts myModule into the current scope, containing all the
exports from the module in the file located in /modules/my-module.js.
suppose we have an export statement like this in our code (call it articleTypes.js) .
export const ARTICLES_FETCHED = 'articles.ARTICLES_FETCHED';
Doing this, Would make actually probably make sense we didn't use export default above
import * as types from './actionTypes'
But If we alter the above lines like this
export default const ARTICLES_FETCHED = 'articles.ARTICLES_FETCHED';
and do something like this
import types from './actionTypes'
Will it work the same way as the above code? or will this actually work? and will this be a better approach?
But If we alter the above lines like this
export default const ARTICLES_FETCHED = 'articles.ARTICLES_FETCHED';
and do something like this
import types from './actionTypes'
Will it work the same way as the above code?
Will it work the same way as the above code?
No, types would be equal to 'articles.ARTICLES_FETCHED'
On the other hand, this module:
export default {ARTICLES_FETCHED: 'articles.ARTICLES_FETCHED'}
would act the same as this module:
export const ARTICLES_FETCHED = 'articles.ARTICLES_FETCHED'
The main benefit from using import * as myVar from 'module' is that you get all the exports from module wrapped up in a neat variable myVar.

How to import nested objects in ES6

Simple question, I'm trying to use electron and I need to get the remote object on the client.
Doing
const {BrowserWindow} = require('electron').remote; // Works
But
import {BrowserWindow} from 'electron/remote' // Does not work
New to ES6 classes just unsure why this is not working. Thanks.
You can only import from modules. electron/remote is not a module, but remote is part of the module electron, so you can write :
import remote from "electron";
And then you can do :
const {BrowserWindow} = remote;
But your first code works fine !
You can read more on import statement here
Hope this helps
I think you're supposed to use it like this:
import {remote} from 'electron'
// do something with remote.BrowserWindow

Categories