Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use npm link with a module written using TypeScript for development?

I am building a lib using TypeScript and Webpack.

To develop this library I have created a separate test project (written using JS) and linked the library using npm link <package-name>.

The problem is that the link leads to the build file and I need to run npm run build every time I make a change.

I want to have a link to the source file and have a live reload. Is it possible? Do I need to write my test project using TS also to make it possible?

Library package.json:

{
  ...
  "main": "lib/qr-code-styling.js",
  "files": [
    "lib"
  ],
  "scripts": {
    "build": "webpack --mode=production"
  },
  ...
}

Code of the library https://github.com/kozakdenys/qr-code-styling/tree/v1

Code of test project https://github.com/kozakdenys/qr-code-styling-site

P.S. I also tried "module": "src/index.ts" in package.json, but it causes an error in the test project Uncaught Error: Cannot find module './core/QRCodeStyling'

like image 276
Denys Kozak Avatar asked Oct 19 '19 10:10

Denys Kozak


People also ask

How do I link npm modules?

Example: Let the local-dir is the local directory and project-dir is the project directory and local_module is the local module package you want to install, first go to the local-dir and type npm link and next go to the project directory and type npm link <local_module> this will link your local module to your project.

Can you use npm packages in TypeScript?

via npm. You can use npm to install TypeScript globally, this means that you can use the tsc command anywhere in your terminal. To do this, run npm install -g typescript . This will install the latest version (currently 4.7).

How does npm link work?

npm link just creates two symbolic links. When you run npm link in a module's root directory, npm creates a symbolic link from your “global node_modules” directory to the local module's directory. The “global node_modules” directory is a special directory where all modules installed with npm install -g are stored.


1 Answers

Another option is to have your TS project automatically rebuild using tsc --watch and then use the compiled code in your project via npm link ../path/to/dep

like image 153
Dana Woodman Avatar answered Sep 28 '22 04:09

Dana Woodman