FlorisSteenkamp/MAT

Not able to use the latest version with node js

Closed this issue · 2 comments

I am trying to use the latest version 2.0.1 with type script in node but getting below error. Was not getting the same error with version 1.1.2 also provided in examples.
I tried using same compiler options as in the example but unable to resolve the issue.
Also tried using module: "ES6" but that is creating some other issues.
Any suggestions on what to do?

const flo_mat_1 = require("flo-mat");
                  ^
Error [ERR_REQUIRE_ESM]: require() of ES Module \node_modules\flo-mat\node\index.js from service.js not supported.
Instead change the require of index.js in service.js to a dynamic import() which is available in all CommonJS modules.

My current TS config

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "strictBindCallApply": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "Node",
    "strictPropertyInitialization": false,
  }
}

The short answer is that since version 2.0.1. require is no longer supported. It is now a totally outdated way of doing things in JavaScript whether in the browser or node.js .

The new standard (for a very long time now) is using ES6 modules. A very detailed explanation of your options and how to resolve your issue is at sindresorhus.

It is unfortunately quite hard to convert a project to ES6 modules but once you're on the other side you will feel much happier :)

You will note in that article that ES6 modules are not compatible with require so my suggestion is definitely to convert your entire project to ES6 modules.

The short answer is that since version 2.0.1. require is no longer supported. It is now a totally outdated way of doing things in JavaScript whether in the browser or node.js .

The new standard (for a very long time now) is using ES6 modules. A very detailed explanation of your options and how to resolve your issue is at sindresorhus.

It is unfortunately quite hard to convert a project to ES6 modules but once you're on the other side you will feel much happier :)

You will note in that article that ES6 modules are not compatible with require so my suggestion is definitely to convert your entire project to ES6 modules.

Thank You :)