blakeembrey / sql-template-tag

ES2015 tagged template string for preparing SQL statements, works with `pg`, `mysql`, and `sqlite`

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Importing error with Typescript.

StefanoOvus opened this issue · comments

I'm experiencing a problem with version 5.0.3 in a Typescript project when importing your lib following the docs, ex:

import sql, { empty, join } from "sql-template-tag";

After transpiling in javascript and running the resulting js with node (version 16 and also 18) file, I get this error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/sql-template-tag/dist/index.js from /app/dist/xxxx.js not supported.
Instead change the require of index.js in /app/dist/xxxx.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/app/dist/xxxx.js:3:28) {
  code: 'ERR_REQUIRE_ESM'
}
{
    "extends": "@tsconfig/node16/tsconfig.json",
    "compilerOptions": {
      "outDir": "dist",
    },
    "include": ["src/**/*"],
    "exclude": ["node_modules", "**/*.spec.ts", "dist"]
  }

I've tried version 4.1.0 of your library and it works as expected.
Thanks in advance.

It’s exactly what the error says. This is an ES Module, not CommonJS, you can’t require an ES Module.

If you’d like to read more, try something like https://simonplend.com/what-you-need-to-know-about-es-modules-in-node-js/. This isn’t an issue with this package.

Thank you, sorry for that.