Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not find a declaration file for module '@gltf-transform/core' [Node.js/Typescript] #824

Closed
aleneum opened this issue Feb 17, 2023 · 8 comments · Fixed by #825 or #827
Closed
Labels
bug Something isn't working

Comments

@aleneum
Copy link

aleneum commented Feb 17, 2023

Describe the bug
@gltf-transform/core and @gltf-transform/extension cannot be imported when using "module": "NodeNext" and "target": "ESNext" in tsconfig.json. You can find the compiler output in the next section. This setup does not work with version 3.0.0 but builds with version 2.5.1 (requires to also install draco3dgltf and its types first).

To Reproduce
I set up a minimal project you can find here

package.json

{
    "type": "module",
    "scripts": {
        "build": "tsc"
    },
    "dependencies": {
        "@gltf-transform/core": "^3.0.0",
        "@gltf-transform/extensions": "^3.0.0"
    },
    "devDependencies": {
        "typescript": "^4.9.5"
    }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "esModuleInterop": true,
    "strict": true,
    "outDir": "build"
  }
}

src/main.ts

import { Document, NodeIO, WebIO } from "@gltf-transform/core";
import { KHRONOS_EXTENSIONS } from "@gltf-transform/extensions";

Steps to reproduce

git clone git@github.com:aleneum/gltf-test.git
cd gltf-test
npm install
npm run  build

Output

src/main.ts:1:41 - error TS7016: Could not find a declaration file for module '@gltf-transform/core'. '/Users/aneumann9/workspace/gltf-test/node_modules/@gltf-transform/core/dist/core.modern.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/gltf-transform__core` if it exists or add a new declaration (.d.ts) file containing `declare module '@gltf-transform/core';`

1 import { Document, NodeIO, WebIO } from "@gltf-transform/core";
                                          ~~~~~~~~~~~~~~~~~~~~~~

src/main.ts:2:36 - error TS7016: Could not find a declaration file for module '@gltf-transform/extensions'. '/Users/aneumann9/workspace/gltf-test/node_modules/@gltf-transform/extensions/dist/extensions.modern.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/gltf-transform__extensions` if it exists or add a new declaration (.d.ts) file containing `declare module '@gltf-transform/extensions';`

2 import { KHRONOS_EXTENSIONS } from "@gltf-transform/extensions";
                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 2 errors in the same file, starting at: src/main.ts:1

Expected behavior
Import should work

Versions:

  • Version: 3.0.0
  • Environment: Node.js (v19.6.0), Typescript (4.9.5)
@aleneum aleneum added the bug Something isn't working label Feb 17, 2023
@donmccurdy
Copy link
Owner

Comparing package.json before/after v3.0 —

The type definitions are there, in dist/core.d.ts for example. The extension of the package.json#main entry changes from .js to .cjs, and I've added a package.json#exports entry which Node.js would likely try to use. I'm not sure how/if either of those should be affecting resolution of the type definitions, though...

@aleneum
Copy link
Author

aleneum commented Feb 17, 2023

I found an issue thread about pony-cause here.
The relevant comment might be this one:

pony-cause didn't list it's types in its exports map at the time of publishing (and didn't have a .d.mts file alongside its .mjs entrypoint). It's since been fixed, though pony-cause still uses the same types for both its require and import entrypoints, which is almost certainly inaccurate, though likely alleviates the error given by the OP.

As you mentioned, your exports field is set. Unfortunately I don't know enough about ESM to have an idea whether
'module-js/ts' files require 'mts' or 'mjs' endings or whether 'ts' files are interpreted as 'mts' by default.

I actually just went down the ESM road because version 3.0.0 cannot be used with Node.JS and 'CommonJS' any longer:

const core_1 = require("@gltf-transform/core");
               ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/aneumann9/workspace/art-viewer/build/backend/node_modules/@gltf-transform/core/dist/core.modern.js from /Users/aneumann9/workspace/art-viewer/build/backend/src/parser.js not supported.
Instead change the require of core.modern.js in /Users/aneumann9/workspace/art-viewer/build/backend/src/parser.js to a dynamic import() which is available in all CommonJS modules.

@donmccurdy
Copy link
Owner

donmccurdy commented Feb 17, 2023

@aleneum thanks! That's helpful — could you try installing the pre-release for #825? CommonJS should still be supported, or that's the goal anyway, but I'm not sure about the .d.mts / .d.cts / .d.ts issue.

npm install --save @gltf-transform/core@next @gltf-transform/extensions@next

@aleneum
Copy link
Author

aleneum commented Feb 18, 2023

Hi @donmccurdy,

sorry for the delay.

could you try installing the pre-release for #825

I did and my package.lock now contains:

"packages": {
        "": {
            "dependencies": {
                "@gltf-transform/core": "^3.0.1-alpha.0",
                "@gltf-transform/extensions": "^3.0.1-alpha.0"
            },
            "devDependencies": {
                "typescript": "^4.9.5"
            }
        },

I update the repo mentioned above accordingly. Running npm run build causes:

(sandbox) gltf-test % npm run build

> build
> tsc

[...]
node_modules/@gltf-transform/extensions/dist/extensions.d.ts:44:15 - error TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

44 export * from './khr-texture-transform';
                 ~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@gltf-transform/extensions/dist/extensions.d.ts:45:15 - error TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

45 export * from './khr-xmp-json-ld';
                 ~~~~~~~~~~~~~~~~~~~


Found 51 errors in 2 files.

Errors  Files
     9  node_modules/@gltf-transform/core/dist/core.d.ts:2
    42  node_modules/@gltf-transform/extensions/dist/extensions.d.ts:2

At this point I had to declare my imports with for instance ./khr-xmp-json-ld.js

Update

I just checked with module: "CommonJS" and removed type: "module" from package.json and this seems to work with 3.0.1-alpha.0 now. I also needed to add two lines to main.ts so that tsc does not strip the imports due to lack of usage.

@donmccurdy
Copy link
Owner

I've published v3.0.1 as a stable release, with the same contents as the @next release above. I'm not sure how to resolve the final errors you're seeing above, I cannot ship imports pointing to explicit .d.ts extensions as that's invalid in most TypeScript setups... hope to find a more satisfying answer here, but it sounds like CommonJS is working OK for now? Did you have to remove type: "module" from your own package.json, or from the package.json installed with this library?

@aleneum
Copy link
Author

aleneum commented Feb 19, 2023

I'm not sure how to resolve the final errors you're seeing above

as far as I understand the solution is to 'type' the imported modules with extensions. This is required for typescript with module settings 'Node16' and beyond. Basically, you need to add 'js' to all imported (typescript) files. So this
export * from './khr-texture-transform'; must be changed to export * from './khr-texture-transform.js';. The weird part: There is no khr-texture-transform.js! That's why this typescript requirement is rather unintuitive if you ask me. I found a discussion about this design decision here but I haven't read it completely. Maybe it contains good reasoning why it has to be like this...

but it sounds like CommonJS is working OK for now?

yes, it does. I'd rather stay with CommonJS since there is more than one tool/workflow that is not compatible with ESM as of now.

Did you have to remove type: "module" from your own package.json

yes, exactly.

Thanks again, great library, great support, great work!

trygveaa added a commit to trygveaa/glTF-Transform that referenced this issue Feb 21, 2023
When importing this library in a project using moduleResolution
nodenext, the types would give errors because the import paths were
missing the file extension which is required for moduleResolution
nodenext. Therefore we have to add .js to all imports of local files.

See developit/microbundle#1019 (comment)
and the issues that comment links to for some more detailed explanation
of this.

I also changed moduleResolution to nodenext in the tsconfig because when
it's set to node, typescript won't give errors for missing file
extensions which means they are easy to forget. With it set to nodenext,
you will get an error if an import is missing the file extension. As far
as I can see, changing this doesn't change the build output.

Depends on donmccurdy/property-graph#70 and
donmccurdy/KTX-Parse#55

Fixes donmccurdy#824
trygveaa added a commit to trygveaa/glTF-Transform that referenced this issue Feb 21, 2023
When importing this library in a project using moduleResolution
nodenext, the types would give errors because the import paths were
missing the file extension which is required for moduleResolution
nodenext. Therefore we have to add .js to all imports of local files.

See developit/microbundle#1019 (comment)
and the issues that comment links to for some more detailed explanation
of this.

I also changed moduleResolution to nodenext in the tsconfig because when
it's set to node, typescript won't give errors for missing file
extensions which means they are easy to forget. With it set to nodenext,
you will get an error if an import is missing the file extension. As far
as I can see, changing this doesn't change the build output.

Depends on donmccurdy/property-graph#70 and
donmccurdy/KTX-Parse#55

Fixes donmccurdy#824
trygveaa added a commit to trygveaa/glTF-Transform that referenced this issue Feb 21, 2023
When importing this library in a project using moduleResolution
nodenext, the types would give errors because the import paths were
missing the file extension which is required for moduleResolution
nodenext. Therefore we have to add .js to all imports of local files.

See developit/microbundle#1019 (comment)
and the issues that comment links to for some more detailed explanation
of this.

I also changed moduleResolution to nodenext in the tsconfig because when
it's set to node, typescript won't give errors for missing file
extensions which means they are easy to forget. With it set to nodenext,
you will get an error if an import is missing the file extension. As far
as I can see, changing this doesn't change the build output.

Depends on donmccurdy/property-graph#70 and
donmccurdy/KTX-Parse#55

Fixes donmccurdy#824
trygveaa added a commit to trygveaa/glTF-Transform that referenced this issue Feb 22, 2023
When importing this library in a project using moduleResolution
nodenext, the types would give errors because the import paths were
missing the file extension which is required for moduleResolution
nodenext. Therefore we have to add .js to all imports of local files.

See developit/microbundle#1019 (comment)
and the issues that comment links to for some more detailed explanation
of this.

I also changed moduleResolution to nodenext in the tsconfig because when
it's set to node, typescript won't give errors for missing file
extensions which means they are easy to forget. With it set to nodenext,
you will get an error if an import is missing the file extension. As far
as I can see, changing this doesn't change the build output.

Depends on donmccurdy/property-graph#70 and
donmccurdy/KTX-Parse#55

Fixes donmccurdy#824
donmccurdy pushed a commit that referenced this issue Feb 22, 2023
When importing this library in a project using moduleResolution
nodenext, the types would give errors because the import paths were
missing the file extension which is required for moduleResolution
nodenext. Therefore we have to add .js to all imports of local files.

See developit/microbundle#1019 (comment)
and the issues that comment links to for some more detailed explanation
of this.

I also changed moduleResolution to nodenext in the tsconfig because when
it's set to node, typescript won't give errors for missing file
extensions which means they are easy to forget. With it set to nodenext,
you will get an error if an import is missing the file extension. As far
as I can see, changing this doesn't change the build output.

Depends on donmccurdy/property-graph#70 and
donmccurdy/KTX-Parse#55

Fixes #824
@donmccurdy
Copy link
Owner

donmccurdy commented Feb 22, 2023

Remaining TypeScript definitions issue should be fixed by @trygveaa's PR #827 – will publish as v3.0.2.

@aleneum
Copy link
Author

aleneum commented Feb 23, 2023

I can confirm that a minimal example of a module-typed package.json with typescript settings module: NodeNext and a non-module-typed package.json with typescript settings module: CommonJS both build and run without issues. Thanks again for the fast resolution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants