Skip to content

Typescript does not properly resolve the viewer API as a module #1

@MikeLuDev

Description

@MikeLuDev

Issue

When attempting to use the viewer API installed via NPM with Typescript, the following error is generated:

image

So what this means is that Typescript is trying to resolve a module in the dir where npm installed Sketchfab - node_modules/@sketchfab/viewer-api/.

The issue when we take a peek into this dir is how the index file is named:

image

Testing

Typescript by default looks to resolve modules that match anything similar to: [index.js, index.d.js, index.ts, index.d.ts] and a few others. You get the point though. When it looks into the Sketchfab dir it doesn't see any JavaScript file that looks like a module export so it ignores it.

To prove this I renamed viewer-api.js to index.js:

image

Then there are no problems:

image

image

image

Resolution

To resolve this, viewer-api.js should be renamed to index.js when published so that tooling can more broadly recognize it as a module index.

Workaround

For anyone experiencing this currently, a quick workaround is to alias the Sketchfab index file using Typescript paths:

// tsconfig.json

{
    "paths":  {
      "@sketchfab/my-viewer-api-alias": ["node_modules/@sketchfab/viewer-api/viewer-api.js", // Add this line
    }
  }
}

You will then be able to import it like so:

import Sketchfab from '@sketchfab/my-viewer-api-alias';

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions