简体   繁体   中英

Can't resolve typescript class from typings file of imported module but works with absolute path

have two node projects as X,Y. X has a typescript class calculator.ts and its exported in index.ts and X's package json has property of typings as below.

"typings": "dist/src/index.d.ts"

node project Y imports calculator as below

 import {Calculator} from 'X'; 

but it throws error

internal/modules/cjs/loader.js:583 throw err; ^

Error: Cannot find module 'X' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)

it works when its imported from absolute path.

 import {Calculator} from 'X/dist/src/calculator'; 

main field of X's package.json should point to the dist/src/index.js to resolve exported classes at run time.

https://docs.npmjs.com/files/package.json#main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM