long.js: TypeScript errors with moduleResolution nodenext

I am having trouble upgrading a package that recently switched a dependency from long@^4 to long@^5:

node_modules/long/umd/index.d.ts:1:18 - error TS1479: The current file is a CommonJS module whose imports will produce ‘require’ calls; however, the referenced file is an ECMAScript module and cannot be imported with ‘require’. Consider writing a dynamic ‘import(“…/index.js”)’ call instead.

1 import Long from “…/index.js”;


Found 1 error(s).

I can suppress the error by skipping library checks in my tsconfig.json file but perhaps this points to a problem with UMD?

Here is my complete tsconfig for reference (with skipLibCheck=false):

{
  "compilerOptions": {
    "module": "CommonJS",
    "resolveJsonModule": true,
    "declaration": false,
    "declarationMap": false,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "lib": ["es6", "dom"],
    "sourceMap": true,
    "allowSyntheticDefaultImports": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "moduleResolution": "nodenext",
    "esModuleInterop": true,
    "incremental": false,
    "skipLibCheck": false,
    "noEmit": false,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "strictBindCallApply": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "allowJs": true,
    "strict": false,
    "types": ["mocha", "chai", "node"]
  },
  "include": ["src/**/*", "hardhat.config.cjs"]
}

https://github.com/stephenh/ts-proto/issues/891

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 17
  • Comments: 16 (1 by maintainers)

Commits related to this issue

Most upvoted comments

It affects Firebase Functions in some cases 💀

Somehow the codebase owner is unwilling to merge my PR #124. I am currently using the following workaround in my package.json to make typescript happy:

  "scripts": {
    ...
    "preprepare": "node -e \"require('node:fs').copyFileSync('./node_modules/long/index.d.ts', './node_modules/long/umd/index.d.ts')\"",
    ...

Fwiw @rjwalters you might consider re-titling this issue (if you’re allowed to as the reporter) to “TypeScript errors with moduleResolution nodenext” b/c I believe that is the core issue here.

Granted, I think it does take a library using import Long = require("..."), but in long v4/CommonJS that was the most kosher way to import a directly-exported const.

Somehow the codebase owner is unwilling to merge my PR #124. I am currently using the following workaround in my package.json to make typescript happy:

  "scripts": {
    ...
    "preprepare": "node -e \"require('node:fs').copyFileSync('./node_modules/long/index.d.ts', './node_modules/long/umd/index.d.ts')\"",
    ...

Thanks for providing this workaround. Hopefully the PR get’s revised/merged, it’s been months. protobufjs also has this package as a dependency so it affects lots of projects