Home > Net >  NodeJS using module's module instead of project's module
NodeJS using module's module instead of project's module

Time:03-05

Trying to use a specific version of a node module (v0.2.0) in my own project, but one of my dependencies requires v0.1.8 of the same module. No issue, I thought, that dependency will use v0.1.8 in its nested node_modules folder and my app will use v0.2.0 in my project's node_modules folder. I added v0.2.0 as a dependency to my package.json and ran npm install again. However, for some reason my app points to the other dependency's v0.1.8 module (in its nested node_modules) when I ctrl-click it, instead of the expected v0.2.0 module correctly installed in the project's own node_modules folder. Here's the code I'm using to import from the modules (@metaplex/js relies on v0.1.8 of @solana/spl-token, while I need to use v0.2.0 of @solana/spl-token in my own app):

import { ... } from "@solana/spl-token";
import { ... } from '@metaplex/js';

I verified that the v0.1.8 spl-token was installed in ./node_modules/@metaplex/js/node_modules/@solana/spl-token, and the v0.2.0 spl-token was installed in ./node_modules/@solana/spl-token. I also ran npm ls @solana/spl-token to make sure it was in the dependency tree, and here are the results:

├─┬ @metaplex-foundation/[email protected]
│ └── @solana/[email protected]
├─┬ @metaplex/[email protected]
│ ├─┬ @metaplex-foundation/[email protected]
│ │ └── @solana/[email protected]
│ ├─┬ @metaplex-foundation/[email protected]
│ │ ├─┬ @metaplex-foundation/[email protected]
│ │ │ └── @solana/[email protected] deduped
│ │ └── @solana/[email protected]
│ ├─┬ @metaplex-foundation/[email protected]
│ │ └── @solana/[email protected]
│ └── @solana/[email protected]
└── @solana/[email protected]

I can't figure out why my code still imports from the wrong version used by another dependency when I installed the correct version in my project's node_modules folder. Is there a workaround or solution to solve this?

fairly new to node, sorry

Edit: (my project is a typescript react project) did some more poking around and apparently, v0.1.8 of the module's package.json's types property points to an *.d.ts file like this:

declare module '@solana/spl-token' {

  export //functions, fields, etc

}

while v0.2.0's package.json's types property points to a simple *.d.ts file like

export * from './instructions/index';
...

(note there is no specific declare keyword for the module itself) and each function has a separate *.d.ts where the specific member is declared, as such in functionName.d.ts:

export declare function functionName(...): Promise<...>;

I don't know if this change causes some type of namespace conflict but it seems like this is a very specific error having to do with this particular module and not something generally wrong with node.

CodePudding user response:

What do you mean by very specific error ?

This Is very common issue At yow tsconfig.json set the baseUrl AND paths

{
...
"baseUrl":"./node_modules",
"paths":{
"name-of-the-package":[
   "relative-to-baseurl/position/where/yow-code-is"
]
}}

The position of yow code Is relative to baseUrl.

  • Related