wooorm / franc

Natural language detection

Home Page:https://wooorm.com/franc/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Franc doesnt work in TypeScript

qertis opened this issue · comments

tsconfig.json

{
  "compilerOptions": {
    "declaration": true,
    "skipLibCheck": true,
    "isolatedModules": true,
    "removeComments": true,
    "esModuleInterop": true,
    "target": "es2022",
    "module": "NodeNext",
    "moduleResolution": "nodenext",
    "outDir": "./dist",
    "allowJs": true
  },
  "include": [
    "lib/**/*"
  ]
}

lib/index.js

import { franc } from 'franc';
export default (query) => {
  let langCode = franc(query, { whitelist: ['eng'] });
  return langCode;
}

After tsc --build

dist/index.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const franc_1 = require("franc");
exports.default = (query) => {
    let langCode = (0, franc_1.franc)(query, { whitelist: ['eng'] });
    return langCode;
};

ERROR:

const franc_1 = require("franc");
                ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/baskovsky/example/node_modules/franc/index.js from /Users/baskovsky/example/dist/index.js not supported.
Instead change the require of /Users/baskovsky/example/node_modules/franc/index.js in /Users/baskovsky/example/dist/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/baskovsky/example/dist/index.js:3:17) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.11.1

I think problem in package.json.
May be replace 'main' to 'module'?

This project works fine in TypeScript.
This project is ESM only. 1 in 10 packages on npm are now ESM only, and it’s rising. You can find many resources online on how to use ESM.
I am not going to change this project.