我正在尝试在 TypeScript 项目中使用带有webpack 2的Globalize 库。typescript/Webpack 2 设置已经工作,但是,在导入和访问 Globalize 时,我在运行 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.
所以我尝试安装全球化类型:
npm install --save-dev @types/globalize
现在我收到以下错误:
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.
不幸的是,这对我来说都是全新的。不知道我是否应该检查 webpack 或 typings 或 globalize 或 typescript ...
这是我的 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"
}
}
和 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());
完整的项目文件(包括 webpack-config)可在此 github 存储库中找到。
注意:这个问题是在尝试解决我之前提出的问题时出现的。如果这可行,它也可以解决我之前的问题。