自从更新到RC.0后,我发现以下构建错误与moment.js(我已经通过npm安装)有关:
[13:44:16] Error: Cannot call a namespace ('moment')
我尝试过以两种方式引用时刻:
import * as moment from 'moment';
import moment from 'moment'
错误是一样的。
问:我做错了什么?这在RC.0
之前有效答案 0 :(得分:3)
您是否已将检查过的所有项目文件加倍检查,因为听起来您可能错过了一个?
尝试从
更改moment-node.d.tsexport = moment; 至 导出默认时刻;
这会暴露任何丢失的文件。 (然后改回来。)
答案 1 :(得分:3)
您需要为tsconfig中的类型添加时刻
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"dom",
"es2015"
],
"module": "es2015",
"moduleResolution": "node",
"target": "es5",
"typeRoots": [
"../node_modules/@types"
],
"types": [
"moment" <====== THIS
]
},
"exclude": [
"node_modules"
],
"compileOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
另外,需要在@ ionic&#39; rollup.config.js中的commonjs中添加对时刻的引用
var ngTemplate = require('../dist/plugins/ng-template').ngTemplate;
var nodeResolve = require('rollup-plugin-node-resolve');
var commonjs = require('rollup-plugin-commonjs');
// https://github.com/rollup/rollup/wiki/JavaScript-API
var rollupConfig = {
useStrict: false,
/**
* entry: The bundle's starting point. This file will
* be included, along with the minimum necessary code
* from its dependencies
*/
entry: './.tmp/app/main.dev.js',
/**
* sourceMap: If true, a separate sourcemap file will
* be created.
*/
sourceMap: true,
/**
* format: The format of the generated bundle
*/
format: 'iife',
/**
* dest: the output filename for the bundle in the buildDir
*/
dest: 'main.js',
/**
* plugins: Array of plugin objects, or a single plugin object.
* See https://github.com/rollup/rollup/wiki/Plugins for more info.
*/
plugins: [
ngTemplate(),
commonjs({
include: [
'node_modules/moment/**'
]
}),
nodeResolve({
module: true,
jsnext: true,
main: true,
browser: true,
extensions: ['.js']
})
]
};
if (process.env.IONIC_ENV == 'prod') {
// production mode
rollupConfig.entry = '.tmp/app/main.prod.js';
rollupConfig.sourceMap = false;
}
module.exports = rollupConfig;
答案 2 :(得分:3)
使用import * as Moment from 'moment';
我收到错误:无法调用命名空间('Moment')。
将其更改为import Moment from 'moment';
解决了问题。
答案 3 :(得分:1)
以下是我用打字稿(2.1.6)和汇总(0.41.4)来完成工作的时间。
要导入时刻,请保持标准方式:
import * as moment from 'moment';
import moment from 'moment';
对于没有默认导出的包是非标准的,它会在运行时导致错误:moment_1.default is not a function
在打字稿中使用时刻与任意时刻一样,并调用default
函数:
var momentFunc = (moment as any).default ? (moment as any).default : moment;
var newFormat = momentFunc(value).format( format );
moment(value).format(format)
会在汇总树震动时导致错误:Cannot call a namespace ('moment')