When attempting to incorporate this library, I encountered the following error:
/Users/alexs/Documents/projects/xml-controller-test/dist/app.controller.js:20
const map_obj_1 = require("map-obj");
^
Error [ERR_REQUIRE_ESM]: The usage of require() with an ES Module /Users/alexs/Documents/projects/xml-controller-test/node_modules/map-obj/index.js from /Users/alexs/Documents/projects/xml-controller-test/dist/app.controller.js is not supported.
Instead, modify the require statement for index.js in /Users/alexs/Documents/projects/xml-controller-test/dist/app.controller.js to utilize dynamic import(), which is available in all CommonJS modules.
at Object.<anonymous> (/Users/alexs/Documents/projects/xml-controller-test/dist/app.controller.js:20:19)
I am hesitant to make any major alterations to my package.json that could disrupt the project. Although I did try adding "type": "module" to package.json, unfortunately, it did not resolve the issue.
ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module due to its '.js' file extension and '/Users/alexs/Documents/projects/xml-controller-test/package.json' containing "type": "module". To treat it as a CommonJS script, rename it with the '.cjs' file extension.
This is how I currently implement it:
import mapObject, { mapObjectSkip } from 'map-obj';
My configuration includes basic nestjs package.json
and tsconfig.json
files.
Is there a method to import this library into a TS project successfully?
The compilation process seems to be where the issue lies. It's surprising that the functionality depends on this setup. Here is an approach I recently attempted:
let mapObject;
import('map-obj').then((m) => {
mapObject = m.default;
});
However, now I am encountering this problem:
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/alexs/Documents/projects/xml-controller-test/node_modules/map-obj/index.js from /Users/alexs/Documents/projects/xml-controller-test/dist/xml-parser.interceptor.js is not supported.
Modify the require statement for index.js in /Users/alexs/Documents/projects/xml-controller-test/dist/xml-parser.interceptor.js to use dynamic import() instead, which is available in all CommonJS modules.
at /Users/alexs/Documents/projects/xml-controller-test/dist/xml-parser.interceptor.js:13:30
tsconfig.ts
{
"compilerOptions": {
// Configuration details
}
}
package.json
{
// Package information and scripts
}
The import statement used is
import mapObject, { mapObjectSkip } from 'map-obj';
For testing purposes, here is how I'm utilizing it:
mapObject(body, (key: string, value) => [key.toLowerCase(), value], {
deep: true,
});