wooorm / franc

Natural language detection
https://wooorm.com/franc/
MIT License
4.07k stars 175 forks source link

Franc doesnt work in TypeScript #116

Closed qertis closed 4 months ago

qertis commented 4 months ago

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
qertis commented 4 months ago

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

wooorm commented 4 months ago

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.

qertis commented 4 months ago

Ok. I created a project for CommonJS only - https://github.com/qertis/franc-node/