long.js icon indicating copy to clipboard operation
long.js copied to clipboard

Unable import in TypeScript since v5+

Open andy1547 opened this issue 2 years ago • 7 comments

Using the latest v5.2.0 package (containing JS and TypeScript index.d.ts), I've tried the following:

Importing with ESM default:

import Long from "long";
import Long from "long/umd";
// these statements give the following error:
TS1259: Module '"node_modules\\long\\index"' can only be default-imported using the 'allowSyntheticDefaultImports' flag

Importing everything with import *, I use this syntax to import CommonJs modules that export a single object (without default) e.g. import * as jquery from 'jquery':

import * as Long from "long";
import * as Long from "long/umd";
TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.

When I change my TypeScript module output from ESM to AMD (AMD output permits the require syntax), it will compile:

import Long = require("long");
import Long = require("long/umd");

// unfortunately I'm unable to use this syntax with ESM output, which is a blocker for us: 
TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.

It's also possible to get this working with v4.0.0 with @types/long v4.0.1 (separate package before types were bundled in v5+) when using the import * as Long from "long" syntax when module output is ESM.

It seems strange to me that the TypeScript declaration exports with = but the JavaScript exports with an ESM default. I suspect the mix/matching of these styles causes the error. The ESM JS/TS should stick to ESM imports/exports and the UMD JS/TS should stick to UMD exports.

andy1547 avatar Mar 22 '22 14:03 andy1547

I was able to import from version 5.0.1 which had export default Long by using import Long from 'long', I could not import it either via import * as Long from 'long', import { Long } from 'long' or import Long from 'long'. With 5.2.0 I had to use import Long = require('long'). Btw, my TsConfig setting is set to { module: 'ESNext' }

owstron avatar Sep 12 '22 18:09 owstron

Is there any fix coming in for this? Looks like google packages like firebase-admin and firebase-tools depend on this module. With the following tsconfig.json,

  "compilerOptions": {
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "module": "ESNext",
    "target": "esnext",
    "moduleResolution": "nodenext",
    "allowJs": true,
    "strict": false,
    "sourceMap": true,
    "outDir": "dist",
    "rootDir": "./src",
    "emitDecoratorMetadata": true,
    "types": ["reflect-metadata"],
    "lib": ["es6", "dom"]
  },
  "lib": ["es2015"],
  "include": ["src/**/*.ts"]

I continue to get the error:

node_modules/@grpc/proto-loader/build/src/index.d.ts:22:23 - error TS1471: Module 'long' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported with 'require'. Use an ECMAScript import instead.

22 import Long = require('long');

creatorapp avatar Jan 30 '23 22:01 creatorapp

The source of this error is: https://github.com/dcodeIO/long.js/blob/3cea40db4c29bc90deab06f0e013467b737db8e4/index.d.ts#L446

This is only compatible with synthetic ES modules being exported as CommonJS. For true ES modules this should be:

export default Long;

See the TypeScript documentation which states that separate type definitions can be specified (scroll to the code bubble right after the "The new support works similarly with import conditions." paragraph).

russellsteadman avatar Apr 15 '23 18:04 russellsteadman

Thanks @russellsteadman for the PR! There should be a new release at 0 UTC. Lmk if there are remaining issues with the fix in place.

dcodeIO avatar Apr 15 '23 21:04 dcodeIO

v5.2.2 shows Module not found: Error: Default condition should be last one when webpack is running. But v5.2.1 does not have this error.

I'm using ESM + Webpack + TS.

magiclen avatar Apr 16 '23 02:04 magiclen

Reordered in v5.2.3, lmk!

dcodeIO avatar Apr 16 '23 09:04 dcodeIO

Any news on this?

nhuethmayr avatar Jan 20 '24 15:01 nhuethmayr