"main" field in package.json is ignored when importing from node-module

I have a Typescript project, let's call it "projectA", which uses a node module, let's call it "@myCompany/moduleB".  

In "moduleB" I have the following structure:

- build
- folder1
- file1.js
- index.js
- index.d.ts
package.json

the package.json of "moduleB" contains the main-attribute to make ./build/index.js the entry file:

{  
"main": "build/index.js",
"name": "@myCompany/moduleB",
"version": "1.0.0"
}

 

In "projectA", I have installed "moduleB" as a dependency using 'npm install @myCompany/moduleB'. When I use a function from "moduleB" in "projectA", WebStorm always adds the '/build' directory to the import path.

So instead of

import { myFunction } from "@myCompany/moduleB";

myFunction();

I get

import { myFunction } from "@myCompany/moduleB/build";

myFunction();

When I manually remove the '/build' from the import path, the code still works as expected, but in WebStorm the syntax-highlighting for 'myFunction()' disappears and I can't do "jump to source" any more.

 

It seems like WebStorm just ignores the file specified in the "moduleB"s package.json. How can I make WebStorm use the cleaner import path without the '/build'-postfix? 
This issues occurs WebStorm and Rider.

Thanks in advance,
Annika

2 comments

Try adding

"typings": "build/index.d.ts",

to the package.json of "moduleB" - does it help?

0

Awesome, Elena! It works like a charm :)

Thanks a lot for your very fast help.

1

Please sign in to leave a comment.