Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "can only be default-imported using the 'esModuleInterop' flag" mean?

I am getting the following error:

lib/app.ts:1:8 - error TS1259: Module '"mongoose-sequence"' can only be default-imported using the 'esModuleInterop' flag

on the following line of code:

import _ from 'mongoose-sequence';

Now, I'm happy to admit I'm missing something with the whole "underscore as a function name" thing, but the error makes no sense, as the esModuleInterop flag is absolutely, positively set to true in my ts.config file.

I am trying to import this:

declare module 'mongoose-sequence' {
  import mongoose = require('mongoose');
  var _: (schema: mongoose.Schema, options?: Object) => void;
  export = _;

from the mongoose-sequence DefinitelyTyped definition.

What am I missing?

like image 774
Nick Hodges Avatar asked Sep 16 '19 15:09

Nick Hodges


People also ask

Can only be default imported esModuleInterop?

The error "Module can only be default-imported using esModuleInterop flag" occurs when we try to import a CommonJS module into an ES6 module. To solve the error, set the esModuleInterop option to true in your tsconfig. json file.

Should I use esModuleInterop?

We highly recommend applying it both to new and existing projects. esModuleInterop is also a recommended option to set in tsconfig. json and when one runs tsc --init it gets set to true automatically.

What is esModuleInterop?

esModuleInterop. Enable TypeScript emitted code to work with code emitted by the Babel ecosystem. This flag is introduced in TypeScript 2.7. Prior to 2.7, when importing commonjs module, you have to do either import x = require('x') or import * as x from 'x' .


1 Answers

Try with: import _ = require('mongoose-sequence');

https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require

like image 89
Johnny Zabala Avatar answered Nov 16 '22 03:11

Johnny Zabala