Script handler.ts
import middy = require('middy');
import middlewares = require('middy/middlewares');
const validator = middlewares.validator;
const handler = middy((event, context, cb) => {
cb(null, {});
});
const schema = {
required: ['body'],
properties: {
body: {
type: 'string'
}
}
};
handler.use(
validator({
inputSchema: schema
})
);
const event = {
body: JSON.stringify({ something: 'somethingelse' })
};
handler(event, null, (err, res) => {
console.log('error', err);
});
Issue when running tsc handler.ts:
node_modules/middy/middlewares.d.ts:4:8 - error TS1259: Module '"C:/XXXXXX/node_modules/middy/index"' can only be default-imported using the 'esModuleInterop' flag
4 import middy from './';
~
node_modules/middy/index.d.ts:55:1
55 export = middy;
~~~
This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
Found 1 error.
Explanation:
middy is exported using export = in index.d.ts
it is being imported using import middy from './'
Reading from Typescript's documentation:
When exporting a module using export =, TypeScript-specific import module = require("module") must be used to import the module.
tsconfigs.js
{
"compilerOptions": {
"lib": ["es2017"],
"removeComments": true,
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"sourceMap": true,
"target": "es2017",
"outDir": "lib",
"esModuleInterop": true,
"module": "commonjs"
},
"include": ["./**/*.ts"],
"exclude": [
"node_modules/**/*",
".serverless/**/*",
".webpack/**/*",
"_warmup/**/*",
".vscode/**/*"
]
}
Possible solution:
Error is resolved when "import middy from './'" in middlewares.d.ts is changed to "import middy = require('./index');"
Why is this closed? What is the solution?
Most helpful comment
Why is this closed? What is the solution?