Ced Ced - 2 months ago 43
Node.js Question

angular 2 directive as npm package

I've a directive that I want to publish on npm. I read the doc here and after that here is what I did:


  • Copy the .js file that is compiled from the .ts file. ( I didn't copy the map file)

  • Make new folder on desktop and paste it there

  • npm init
    and
    npm publish

  • create new project and
    npm install --save-dev
    my published package



However it doesn't find the name of the directive when I'm trying to declare it in the module

@NgModule({
declarations: [
AppComponent,
MyDirective //this is not found
],


The js file appears in the module folder though

Answer

You imported import {...} from 'your-package-name/your-main-js'; it, right?

Here's a nice guide to create npm packages for Angular2.

https://medium.com/@OCombe/how-to-publish-a-library-for-angular-2-on-npm-5f48cdabf435#.coog6uf98

If you want to create a component package, remember to inline your templates/styles ! Otherwise your app will be broken..

Or you could use a script like this https://github.com/ludohenin/gulp-inline-ng2-template to inline those templates/styles..

Maybe this repo as a starting point will help: https://github.com/mxii/ng2-offclick