Reputation: 4972
I am trying to use the Globalize library with webpack 2 in a TypeScript project. The typescript/Webpack 2 setup already works, however, when importing and accessing Globalize, I am getting the following error message when running webpack:
ERROR in ./.tmp-globalize-webpack/C--Projects-webpack2-testing-app-index.ts
(1,1): error TS2304: Cannot find name 'module'.
ERROR in ./app/index.ts
(2,23): error TS7016: Could not find a declaration file for module 'globalize'. 'C:\Projects\webpack2-testing\node_modules\globalize\dist\node-main.js' implicitly has an 'any' type.
So I tried installing the globalize types:
npm install --save-dev @types/globalize
Now I get the following error:
ERROR in ./.tmp-globalize-webpack/C--Projects-webpack2-testing-app-index.ts
(1,1): error TS2304: Cannot find name 'module'.
ERROR in ./app/index.ts
(2,23): error TS2306: File 'C:/Projects/webpack2-testing/node_modules/@types/globalize/index.d.ts' is not a module.
Unfotunately this is all very new to me. Don't know if I should check webpack or typings or globalize or typescript...
This is my package.json:
{
"name": "webpack2-testing",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config webpack-config.js"
},
"devDependencies": {
"cldr-data": "^30.0.4",
"globalize": "^1.2.2",
"globalize-webpack-plugin": "^0.3.10",
"html-webpack-plugin": "^2.28.0",
"ts-loader": "^2.0.0",
"typescript": "^2.1.6",
"webpack": "^2.2.1"
}
}
and the index.ts:
import Globalize from "globalize";
function component () {
let element = document.createElement('div');
let currencyFormatter = Globalize.currencyFormatter( "USD" );
element.innerHTML = currencyFormatter( 69900 );
return element;
}
document.body.appendChild(component());
The complete project files (including webpack-config) are available at this github repository.
Note: This question arose while trying to solve a question I asked previously. If this works, it could also resolve my previous question.
Upvotes: 2
Views: 5228
Reputation: 21
I finally got it to work:
In your globalize.d.ts
file:
declare module 'globalize' {
export = Globalize;
}
In your webpack config:
plugins: [
new webpack.ProvidePlugin({
Globalize: "globalize"
})
]
And finally, when you import globalize
in all your .ts
files:
import * as Globalize from 'globalize';
Upvotes: 2
Reputation: 529
I did the following steps to use globalize.js in typescript project.
1. Install the library
npm install globalize
npm install @types/globalize
2. Create a file to export module (globalize.d.ts)
/// <reference types='globalize' />
/* tslint:disable */
declare namespace globalize { }
declare module 'globalize' {
export default Globalize;
}
3. Use in my main file
import globalize from 'globalize';
// Formatter creation.
var formatter = globalize.numberFormatter();
// Formatter execution (roughly 10x faster than above).
formatter( Math.PI );
However, I never success download the cldr lib, so my browser has errors like:
./src/client/index.tsx
(5,23): error TS2306: File '/Users/alen/Workspace/Qunhe/core/node_modules/@types/globalize/index.d.ts' is not a module.
./~/globalize/dist/globalize.js
Module not found: Error: Cannot resolve module 'cldr' in /Users/alen/Workspace/Qunhe/core/node_modules/globalize/dist
@ ./~/globalize/dist/globalize.js 22:2-25:14
./~/globalize/dist/globalize.js
I'm not sure it's the globalize.js issue or cldr.js issue, still trying. Sorry I made the early reply other than solve your question.
Edit:
To solve the error above, I use the webpack plugin to help me pack data, and install the cldr-data. and it did works.
and use in webpack.config.js as demo illustrate here. https://github.com/globalizejs/globalize/tree/master/examples/app-npm-webpack
I have to install using
npm install globalize cldr-data
And back in browser, every thing works well.
Upvotes: 2