Using npm modules with ionic 2


#1

Hello,

I am having trouble with npm mpdules in ionic 2. I think it has to do with import vs require.
I am trying to use a simple npm package called country-data and after installing using npm I import it in my code

import * as CountryData from "country-data";

but the data is empty so I looked into the module and it is using require to load the data from json files
var currenciesAll = require('./data/currencies.json');
if I preint that variable in node console it outputs the correct values
but it fails to do so in the generated app.bundle.js

here is the output from ionic info

Cordova CLI: 5.4.1
Ionic Version: 2.0.0-alpha.57
Ionic CLI Version: 2.0.0-beta.17
Ionic App Lib Version: 2.0.0-beta.8


#2

One thing you might try is installing json-loader:

$ npm install json-loader

…and then configuring webpack.config.js to use it for all .json files with a stanza something like this:

 module: {
    loaders: [
[...]
{
        test: /\.json$/,
        loader: 'json'
}
[...]
    ]
}

#3

thanks a lot. it worked!:grin: