Friday, June 17, 2016

Use local changes of node package with TypeScript

Leave a Comment

I'm very new to using npm and TypeScript so I'm hoping that I'm missing something obvious. We have authored a node package written in TypeScript for our own internal use. So, the source is written in Typescript, and the file structure looks like this:

src/myModule.ts myModule.ts 

Where myModule.ts looks like this:

export * from "./src/myModule"; 

I then run tsc to generate .js and .d.ts files. So, the files then look like this:

src/myModule.js src/myModule.ts src/myModule.d.ts myModule.js myModule.ts myModule.d.ts 

All of this gets pushed to git and then our main app includes this package as a dependency via a git URL. When I first attempted this, I got an error like this:

export * from "./src/myModule"; ^ ParseError: 'import' and 'export' may appear only with 'sourceType: module' 

After some searching around, I found that the issue was with the .ts files that were getting loaded in. So I added the following to my .npmignore:

*.ts !*.d.ts 

After doing this, it brings in the node package without any problems.

The problem I am running into is that I want to be able to run off of local changes to the node package during active development. I want to be able to make changes in the node package, and then have the main app pick up these changes so that I can make sure everything works together before pushing to git. Everything I find says to use npm link. This makes it point to my local directory as expected, but the problem is that it now has all the .ts files, and the same errors show up.

I'm sure npm link works great when everything is written in JavaScript, but we are using TypeScript to write everything. Is there a way to make this work with TypeScript?

1 Answers

Answers 1

This makes it point to my local directory as expected, but the problem is that it now has all the .ts files, and the same errors show up.

The errors will only show up if you have those .ts files and a seperate declaration for those files.

Recommended fix

Just use the seperate .ts/.js/.d.ts files and steer clear of bundling a single .d.ts.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment