Unable to import date-utils module in typescript

Asked 2 years ago, Updated 2 years ago, 117 views

import* asdt from 'date-utils'

Occurs simply when importing in the same way as above.

Based on the content of the error, I don't think there is a problem with tconfig.json, but what is the cause?

Error: (1,21) TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and reference it default export.

The following tconfig.json contents

{
  "compilerOptions": {
    "moduleResolution": "node",
    "skipLibCheck": false,
    "target": "es5",
    "module": "commonjs",
    "lib": ["es2017", "dom",
    "experimentalDecorators": true,
    "allowJs"—false,
    "jsx": "react",
    "sourceMap": true,
    " strict"—true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters"—true,
    "noFallthroughCasesInSwitch": true,
    "esModuleInterop": true,
    "removeComments": true,
    "newLine": "LF",
    "downlevelIteration": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports"—false
  },
  " exclude": ["node_modules" ]
}

node.js typescript

2022-09-30 18:19

1 Answers

This error message points out that you must import date-utils using the syntax for importing default export.In other words, the error should disappear by doing the following:

import dt from 'date-utils'

In addition to the above, the error did not disappear without removing the "allowSyntheticDefaultImports":false setting from tsconfig.json.

Both esModuleInterop and this allowSyntheticDefaultImports are related options when treating the commonjs module as an ESModule, with the difference that the former supports runtime and the latter only on the type system.

When esModuleInterop is checked, allowSyntheticDefaultImports is also automatically turned on, but in this configuration, allowSyntheticDefaultImports was explicitly turned off, which seems to have caused an error on the type system.

There is no reason to turn off allowSyntheticDefaultImports if you are interested in loading the commonjs module, and this will resolve the issue.

The request-promise module also does not have a default export in the type definition file and does not have multiple export statements, but does not know what is the difference from the import*from 'request-promise' that can be used without problems.

This is affected by the difference that both modules export objects with export=, while date-utils exports functions, while request-promise exports objects only.
The syntax import*asdt from 'date-utils' on the ES module causes an inconsistency with what date-utils exports because dt is just an object that is not a function (more precisely a module name space exotic object).
If you read request-promise to a import*asrp from 'request-promise', then if rp is an exported (only) object from the module, there will be no inconsistencies on the type system, so no errors will occur.


2022-09-30 18:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.