35

I'm trying to include a local library, so i follow theese 2 tutorials: how to create library, how to consume local library. So, i have a nice sample library (it has package.json, index.ts, etc), i try to include it in my main project (npm link works fine, i can see a symlink to my lib), but i cant refer it from my main project.

import { HelloWorld } from "my-test-lib"; // "Cant find module"

Also, i tried to install it via npm install ../libs/my-test-lib, but results are the same: "Cant find module". Any suggestions on how to include it to my project?

PS: i'd prefer something like npm install, so i'd be able to commit this change to the repository (as opposing to npm link).

EDIT (why its not a duplicate): in how to specify local modules as npm package dependencies its not clear which way to go: both "bar":"file:../foo/bar" and npm link are not working for me for the reason mentioned above.

EDIT2: Okay, i've tried "preinstall": "npm ln my-test-lib ../libs/my-test-lib" in the package.json. And then import { HelloWorld } from "my-test-lib";. Still the same error "Cannot find module". Can it be something wrong with the package itself?

EDIT3: So yes, the main point of this question is to figure out why

// package.json
"dependencies": {
  "my-test-lib": "file:../lib/my-test-lib"
}

// ts file
import { HelloWorld } from "my-test-lib";

is not working. Wondering, how can i debug that.

Vitalii Vasylenko
  • 4,776
  • 5
  • 40
  • 64
  • Could you include you local library for reference. – HerbN Oct 18 '17 at 19:49
  • Yep, i see a line "my-test-lib": "file:../libs/my-test-lib" in the package.json, but still i cant use it for the same error "Cant find module". – Vitalii Vasylenko Oct 18 '17 at 19:50
  • Instead of adding EDIT statements if you have figured out that "So yes, the main point of this question is to figure out why" maybe edit the question to contain only that -- the minimal representation of the question without anything extra. – Joel Peltonen Oct 20 '17 at 16:00
  • Nope, practice shows that some people who already answered might be confused, so its always better to keep old version of the question (changes to fix typos, mistakes, formatting, etc are ok). Probably its better just to start a new question. – Vitalii Vasylenko Oct 21 '17 at 08:36
  • 13
    Not a duplicate. I have same problem of trying to *use* a locally installed module after following the advice of the supposed duplicate. – Stan James Dec 23 '17 at 21:13
  • 1
    Did you find a solution for this @VitaliiVasylenko? – TommyKey Jan 05 '18 at 14:50
  • 1
    @TommyKey Unfortunately, not for my OP question. After digging around, i decided to include local library's source code into the project, as apparently it was supposed to change quite often. – Vitalii Vasylenko Jan 07 '18 at 13:28
  • This approach works for me just good. What is your main script in package.json? It should point to file where you export your function HelloWorld. It also must match your name in package.json with destination in import statement. –  Nov 02 '18 at 15:01
  • @JaroslavZavodny I have the same problem as everyone here... do you mind sharing an example of what you did? I don't understand how to import modules from the local package? – lonix Feb 26 '19 at 07:10
  • @VitaliiVasylenko the problem is in your exported module. You should have a main directive in the package.json of your exported module pointing to the compiled main.js (not main.ts). This article explains everything : https://itnext.io/step-by-step-building-and-publishing-an-npm-typescript-package-44fe7164964c – Vincent J Mar 30 '19 at 09:05
  • 2
    For me, the problem was that I didn't run `yarn build` in the linked library's folder. Therefore I didn't have a `dist` folder in the package. – loc Nov 17 '22 at 11:36
  • @loc's comment solved my problem. I had to first build the local page with `npm run build` (to create the dist folder) and then update this package in the main project with `npm update `. – Petri Jan 03 '23 at 05:16

1 Answers1

-4
import defaultExport from './local-file'

Or

import { namedExport } from './local-file'
Funk Soul Ninja
  • 2,113
  • 3
  • 17
  • 27
  • 1
    Thanks for fast answer. That might work for a single file - but i'm looking how to make it work for the whole library. I'll check that though. – Vitalii Vasylenko Oct 18 '17 at 19:55
  • 1
    Also, import { HelloWorld } from "../../../../../lib/my-test-lib"; is not looking cool at all – Vitalii Vasylenko Oct 18 '17 at 19:56
  • Although, yes, it works for a single file. Thanks, i'll upvote it - but will not mark it as an answer, because its not answering the question. – Vitalii Vasylenko Oct 18 '17 at 20:03
  • @VitaliiVasylenko trying using webpack's resolve. https://webpack.js.org/configuration/resolve/ – Funk Soul Ninja Oct 18 '17 at 20:58
  • Yeah, probably will give it a shot. Though, i'm not using webpack in my project (at least, so far), so i'd prefer not to include any extra 3rd party tools - especially to do the stuff that is supposed to work out-of-box. – Vitalii Vasylenko Oct 18 '17 at 21:04
  • @VitaliiVasylenko perhaps it would be easier if you simply exposed any variables or functions you need to the global scope. I personally can't think a time where I wouldn't use Webpack. Maybe I'm wrong, but It feel like it's the industry standard. – Funk Soul Ninja Oct 18 '17 at 21:17
  • 1
    Might be.. we have a quite specific project (its a WebGl-oriented), so far it goes good without Webpack (i'm not excluding we will need Wepback at some moment in future). Anyway, i'm wondering why `npm install ../libs/my-test-lib` is just not working. Probably i should rephrase the question... Thanks for your help anyway! – Vitalii Vasylenko Oct 18 '17 at 21:22
  • @VitaliiVasylenko you could use Webpack as you develop. Setting it up would take less than a few minutes. – Funk Soul Ninja Oct 21 '17 at 20:15