3

I am developing application using Angular2. I am trying to import and use external library 'benchmark.js'. So, I install it using 'npm i --save benchmark'. The dependencies of package.json is like following.

 "name": "angular2-webpack",
  "version": "1.0.0",
  "description": "A webpack starter for Angular",
  "scripts": {
    "start": "webpack-dev-server --inline --progress --port 8080",
    "test": "karma start",
    "build": "rimraf dist && webpack --config config/webpack.prod.js --progress --profile --bail"
  },
  "license": "MIT",
  "dependencies": {
    "@angular/common": "~2.3.0",
    "@angular/compiler": "~2.3.0",
    "@angular/core": "~2.3.0",
    "@angular/forms": "~2.3.0",
    "@angular/http": "~2.3.0",
    "@angular/platform-browser": "~2.3.0",
    "@angular/platform-browser-dynamic": "~2.3.0",
    "@angular/router": "~3.3.0",
    "benchmark": "^2.1.3",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-rc.4",
    "systemjs": "0.19.40",
    "zone.js": "^0.7.2"
  },

And then, I run 'npm install', if I look at the node_modules, there is benchmark directory. But when I tried to import using 'import Suite from 'benchmark'', it gives me error like 'Cannot fine module 'benchmark'. I thought I can import the Suite object like Observable from rxjs. I applied the exactly same way I did for Observable. Anyway, how I am able to use benchmark.js in my component.

Anna Lee
  • 909
  • 1
  • 17
  • 37
  • 1
    If worse comes to worse, you can `require` instead of `import`. Depending on your typescript version, `import` may have some problems with non-TS modules. – Seiyria Jan 30 '17 at 18:29
  • I am using "typescript": "~2.0.10", could you give some example how to require the library and use it? – Anna Lee Jan 30 '17 at 19:12
  • It may be worth trying 2.1, as I believe that's when it changed. But, to require: instead of `import X from 'benchmark'` you'll want to do `const X = require('benchmark')`. – Seiyria Jan 30 '17 at 19:30
  • Actually, when I run 'npm start', I got the error like below. Module parse failed: \angular2\node_modules\benchmark\package.json Unexpected token (2:9) You may need an appropriate loader to handle this file type. SyntaxError: Unexpected token (2:9) – Anna Lee Jan 30 '17 at 19:37
  • Weird. I'd still try upgrading typescript if you can, and if that doesn't solve anything, it may not be a bad idea to post your webpack config in the OP (provided it's relatively simple). – Seiyria Jan 30 '17 at 19:41

0 Answers0