nktnet1/import-sync

Unable to tic in CDK project

Closed this issue · 3 comments

I have an AWS CDK project which fails to tsc because of this:

(venv) rodrigocarvajal@rodrigos-MBP BotFactory % tsc
node_modules/import-sync/dist/cjs/types.d.ts:1:22 - error TS6053: File '/Users/rodrigocarvajal/Documents/projects/BotFactory/node_modules/import-sync/src/httptoolkit-esm-types.d.ts' not found.

1 /// <reference path="../../src/httptoolkit-esm-types.d.ts" />
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/import-sync/dist/cjs/types.d.ts:2:17 - error TS7016: Could not find a declaration file for module '@httptoolkit/esm'. '/Users/rodrigocarvajal/Documents/projects/BotFactory/node_modules/@httptoolkit/esm/esm.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/httptoolkit__esm` if it exists or add a new declaration (.d.ts) file containing `declare module '@httptoolkit/esm';`

2 import esm from '@httptoolkit/esm';
                  ~~~~~~~~~~~~~~~~~~


Found 2 errors in the same file, starting at: node_modules/import-sync/dist/cjs/types.d.ts:1

(venv) rodrigocarvajal@rodrigos-MBP BotFactory % 

My tsconfig.json:

{
    "compilerOptions": {
        "target": "ES2020",
        "module": "commonjs",
        "lib": [
            "es2020",
            "dom"
        ],
        "declaration": true,
        "strict": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "noUnusedLocals": false,
        "noUnusedParameters": false,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": false,
        "inlineSourceMap": true,
        "inlineSources": true,
        "experimentalDecorators": true,
        "strictPropertyInitialization": false,
        "typeRoots": [
            "./node_modules/@types"
        ]
    },
    "exclude": [
        "node_modules",
        "cdk.out"
    ]
}

My package.json:

{
    "name": "bot-factory",
    "version": "0.1.0",
    "bin": {
        "bot-factory": "bin/bot-factory.js"
    },
    "scripts": {
        "build": "tsc",
        "watch": "tsc -w",
        "test": "cdk synth && npm run build && jest",
        "cdk": "cdk",
        "lint": "eslint --ext .ts lib/ --fix --ignore-pattern \"*.d.ts\" && eslint --ext .ts bin/ --fix --ignore-pattern \"*.d.ts\" && eslint --ext .ts test/ --fix --ignore-pattern \"*.d.ts\""
    },
    "devDependencies": {
        "@types/jest": "^29.5.5",
        "@types/node": "20.7.1",
        "@typescript-eslint/eslint-plugin": "^6.17.0",
        "@typescript-eslint/parser": "^6.17.0",
        "aws-cdk": "^2.132.0",
        "eslint": "^8.56.0",
        "eslint-config-airbnb-base": "^15.0.0",
        "eslint-plugin-import": "^2.29.1",
        "jest": "^29.7.0",
        "ts-jest": "^29.1.1",
        "ts-node": "^10.9.1",
        "typescript": "~5.2.2"
    },
    "dependencies": {
        "@aws-cdk/aws-lambda-python-alpha": "^2.132.0-alpha.0",
        "@httptoolkit/esm": "^3.3.0",
        "aws-cdk-lib": "^2.132.0",
        "constructs": "^10.0.0",
        "source-map-support": "^0.5.21",
        "import-sync": "^2.2.0"
    }
}

In my code I am simply doing:

        const plug = importSync(props.processingStackClassFile);
        const constructorName = Object.keys(plug)[0];

And it works, I can even do a cdk synth and deploy, however tsc fails. Am I doing something wrong here?

Hi @ksco92,

Thank you for reporting the issue.

Are you able to provide a Code Sandbox or Replit with a minimal reproducible example?

I am unable to recreate it myself.

For example, you can create a fork of:

I've basically copied your tsconfig.json and package.json and tried to run tsc with your build script, with the following content:

src/index.ts

const importSync = require('import-sync');

const { sum } = importSync('./example.ts');
console.log('1 + 1 =', sum(1, 1));

src/example.ts

export const sum = (a: number, b: number) => a + b;

and it ran without errors.

Also tagging @pimterry if you have any ideas.

The file referenced here (import-sync/src/httptoolkit-esm-types.d.ts) genuinely doesn't exist in the published package (see https://unpkg.com/browse/import-sync@2.2.0/ - src is not included at all) and indeed is referenced from types.d.ts (see https://unpkg.com/browse/import-sync@2.2.0/dist/cjs/types.d.ts).

I think that's the problem - if these types are intended to be used downstream, they should be included in the public package.

I'm not sure why different TypeScript configurations are passing/failing here, but including that file (or changing the setup so it's no longer referenced) seems like a sensible fix regardless.

Thank you @pimterry! I've made a patch in #81.

Hi @ksco92, I believe the issue should be resolved in version 2.2.1. Please update the package with

npm install import-sync@latest

If the issue persists, feel free to let us know and we will reopen the issue.