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

Cannot find module '../index.node' or its corresponding type declarations. #1069

Closed
bruno-sartori opened this issue Sep 21, 2024 · 3 comments

Comments

@bruno-sartori
Copy link

I'm sorry if my problem is too silly. I've only been using Rust for a short time and I'm still having beginner problems.

I encountered an issue when creating a new project with neon using the following steps:

  1. Created a new neon project with the command:

    npm init neon test
  2. Selected "app" option on prompt

  3. Updated Cargo.toml to set a custom path for rust files:

    [lib]
    path = "lib/lib.rs"
  4. Added a new index.ts file under the src directory.

  5. Build using npm run cargo-build -- --release which executes cargo build --message-format=json > cargo.log. I also tried with cargo-cp-artifact -nc lib.node -- cargo build --message-format=json-render-diagnostics.

  6. Tried to import the .node file in index.ts like this:

    import lib from '../index.node';

However, when running the code, I received the following error:

Cannot find module '../index.node' or its corresponding type declarations.

It seems that the module resolution is failing to locate the .node file. Could this be related to the change in the lib path within Cargo.toml, or is there a step missing in the build process when using a custom library path?

Expected Behavior:
The index.node file should be correctly found and imported without throwing a module resolution error.

Environment:

  • Node.js version: 20.16.0
  • @neon-rs/cli version: 0.1.73
  • Neon Version: 1
  • Cargo version: 1.81.0
  • Operating System: Ubuntu 20.04.6 LTS
@kjvalencik
Copy link
Member

I don't think it's related to moving the Rust source. Could it be the TypeScript config not understanding native modules or resolving to the wrong place? Does it work with vanilla JS?

@kotekpsotek
Copy link

@bruno-sartori Your error is pined up to difference between CommonJS (common node.js) syntax and ES Modules syntax, describes how elements to be called wihin your project. Among that strongly associated with TypeScript Type awareness.

**I've made for you and everybody with same error repository contains solution guideline: https://github.com/kotekpsotek/napi-ts-example
When you've another trouble, feel free to ask here or as repo issue 👍

@kjvalencik As you've said this is not directly related to Neon_Bindings but to interactions from TypeScript project, nonetheless the Neon_Bindings should include examples covering such cases, TypeScript is industrial match.
Yes, this is related to Es-Modules syntax doesn't understad the files with .node extension ... so we must play trick with her.

VanillaJs in terms of Node.js uses CommonJS require() syntax which understands perfectly .node files purpose, but you still stay without hinters for your code editor what is inside

@bruno-sartori
Copy link
Author

@kjvalencik @kotekpsotek Sorry for the late reply.
@kotekpsotek I followed the steps in your example and it worked perfectly! Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants