Join the Stack Overflow Community
Stack Overflow is a community of 6.2 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

My goal is to use an external JavaScript Library in an Angular 2 project. To do so, there are two ways:

1) Declare those variables in my files (they will be present in the environment during runtime) like this:

declare var numbro: any; // in my Component

And I added the numbro.js file like this in the index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>APP Angular 2</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="../node_modules/numbro/numbro.js"></script>
    <base href="/">
  </head>
  <body>
    <my-app>Loading...</my-app>
  </body>
</html>

2) The other solution is to use their declaration files in TypeScript (if they already exist)

The problem here is that not all JavaScript libraries support TypeScript and it didn't seem a good idea to create declaration files for each library I want to use in my project. Therefore, I decided to adopt the first way. Everything seemed good until I faced this error:

ORIGINAL EXCEPTION: require is not defined

Here is my webpack configuration:

'use strict';

const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const merge = require('webpack-merge');
const webpack = require('webpack');

const communConfig = require('./webpack-commun.config.js');

module.exports = function(options) {
  const hotModuleReplacementPlugin = new webpack.HotModuleReplacementPlugin();
  const commonsChunkPlugin = new CommonsChunkPlugin({
    name: [ 'vendor', 'polyfills' ]
  });
  const htmlPlugin = new HtmlWebpackPlugin({
    template: options.html
  });

  return merge(communConfig(options), {
    entry: {
      polyfills: options.polyfills,
      vendor: options.vendor,
      app: [
        `webpack-dev-server/client?http://localhost:${options.port}`,
        'webpack/hot/only-dev-server',
        options.entry
      ]
    },

    output: {
      path: options.outputDir,
      publicPath: `http://localhost:${options.port}`,
      filename: '[name].js',
      chunkFilename: '[id].chunk.js'
    },

    plugins: [
      hotModuleReplacementPlugin,
      commonsChunkPlugin,
      htmlPlugin
    ]
  });
};

Here is my vendor.ts file:

import 'rxjs';
import 'core-js/es6';
import 'zone.js/dist/zone';

import '@angular/common';
import '@angular/compiler';
import '@angular/core';
import '@angular/http';
import '@angular/platform-browser';
import '@angular/platform-browser-dynamic';
import 'numbro';

And Here is my pollyfills.ts:

import 'es6-shim';
import 'reflect-metadata';
import 'ts-helpers';

Is there a way to resolve this?

share|improve this question
    
Do you use system.js in your project? – galvan Sep 22 at 17:28
    
What version of angular 2 are you using? You can find that in package.json file. - for example mine says: "@angular/common": "2.0.0", . So is 2.0 final release - no RC candidates here. If you are using the angular CLI please mention because is a hole other answer. At first glance you don't have webpack setup but i can't help you without more info. – AIon Sep 22 at 17:43
    
@galvan No i didn't use system.js in my project – Ibti Sep 23 at 9:51
    
@AIon Yes i work with the final release 2.0 , I didn't use angular CLI – Ibti Sep 23 at 9:56
    
@AIon I added the webpack file can you please have a look :) Thanks – Ibti Sep 23 at 10:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.