Gruntjs 错误:模块名称";幻影“;尚未加载上下文:\的。使用require([])
我正在尝试使用karma和grunt运行phantom.js测试,我得到:Gruntjs 错误:模块名称";幻影“;尚未加载上下文:\的。使用require([]),gruntjs,requirejs,phantomjs,karma-runner,Gruntjs,Requirejs,Phantomjs,Karma Runner,我正在尝试使用karma和grunt运行phantom.js测试,我得到: phantomjs2.1.1(macosx0.0.0)错误 错误:尚未加载上下文的模块名“网页”:。使用require([]) http://requirejs.org/docs/errors.html#notloaded 在vendor/bower/requirejs/require.js:140处 Grunfile.js中的karma配置如下所示: karma: { options: {
phantomjs2.1.1(macosx0.0.0)错误
错误:尚未加载上下文的模块名“网页”:。使用require([])
http://requirejs.org/docs/errors.html#notloaded
在vendor/bower/requirejs/require.js:140处
Grunfile.js中的karma配置如下所示:
karma: {
options: {
basePath: "./",
singleRun: true,
captureTimeout: 7000,
autoWatch: true,
reporters: karmaOptions.reporters,
browsers: ["PhantomJS", "PhantomJS_custom"],
// you can define custom flags
customLaunchers: {
'PhantomJS_custom': {
base: 'PhantomJS',
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
},
},
flags: ['--load-images=true'],
debug: true
}
},
phantomjsLauncher: {
// Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
exitOnResourceError: true
},
files: [ "node_modules/karma-jasmine-async/src/jasmine.async.js", "vendor/bower/requirejs/require.js", "vendor/bower/underscore/underscore.js", "node_modules/phantomjs-prebuilt/bin/phantom.js", "test/runner.js",
{
pattern: "<%= rootFolderName %>/**/*.*",
included: false
},
{
pattern: "test/specs/*.*",
included: false
},
{
pattern: "test/mocks/*.*",
included: false
},
{
pattern: "vendor/**/*.js",
included: false
},
{
pattern: "vendor/**/*.html",
included: false
},
{
pattern: "vendor/**/*.xml",
included: false
}
],
exclude: [
'dist/**',
'vendor/**/*Spec.js'
]
},
// This creates a server that will automatically run your tests when you
// save a file and display results in the terminal.
daemon: {
options: {
singleRun: false
}
},
// This is useful for running the tests just once.
run: {
options: {
singleRun: true
}
},
run_functional: {
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
}
}
}
console.log("starting");
var webPage = require('webpage');
var page = webPage.create();
page.windowName = 'my-window';
page.settings.webSecurityEnabled = false;
page.open('http://google.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
debugger;
console.log("success");
}
phantom.exit();
});
require.config({
paths: {
"vendor": "vendor",
"node_modules": "node_modules",
"almond": "node_modules/almond/almond",
"underscore": "node_modules/lodash/dist/lodash.underscore",
"jquery": "node_modules/jquery/jquery",
"jquery-ui": "node_modules/jquery-ui-dist/jquery-ui",
"backbone": "node_modules/backbone/backbone",
"layoutmanager": "node_modules/layoutmanager/backbone.layoutmanager",
"backbone.subroute": "node_modules/backbone.subroute/backbone.subroute",
"bootstrap": "node_modules/bootstrap/dist/js/bootstrap",
"i18next": "node_modules/i18next-client/i18next.amd",
"text": "node_modules/requirejs-text/text",[...]
test.js如下所示:
karma: {
options: {
basePath: "./",
singleRun: true,
captureTimeout: 7000,
autoWatch: true,
reporters: karmaOptions.reporters,
browsers: ["PhantomJS", "PhantomJS_custom"],
// you can define custom flags
customLaunchers: {
'PhantomJS_custom': {
base: 'PhantomJS',
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
},
},
flags: ['--load-images=true'],
debug: true
}
},
phantomjsLauncher: {
// Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
exitOnResourceError: true
},
files: [ "node_modules/karma-jasmine-async/src/jasmine.async.js", "vendor/bower/requirejs/require.js", "vendor/bower/underscore/underscore.js", "node_modules/phantomjs-prebuilt/bin/phantom.js", "test/runner.js",
{
pattern: "<%= rootFolderName %>/**/*.*",
included: false
},
{
pattern: "test/specs/*.*",
included: false
},
{
pattern: "test/mocks/*.*",
included: false
},
{
pattern: "vendor/**/*.js",
included: false
},
{
pattern: "vendor/**/*.html",
included: false
},
{
pattern: "vendor/**/*.xml",
included: false
}
],
exclude: [
'dist/**',
'vendor/**/*Spec.js'
]
},
// This creates a server that will automatically run your tests when you
// save a file and display results in the terminal.
daemon: {
options: {
singleRun: false
}
},
// This is useful for running the tests just once.
run: {
options: {
singleRun: true
}
},
run_functional: {
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
}
}
}
console.log("starting");
var webPage = require('webpage');
var page = webPage.create();
page.windowName = 'my-window';
page.settings.webSecurityEnabled = false;
page.open('http://google.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
debugger;
console.log("success");
}
phantom.exit();
});
require.config({
paths: {
"vendor": "vendor",
"node_modules": "node_modules",
"almond": "node_modules/almond/almond",
"underscore": "node_modules/lodash/dist/lodash.underscore",
"jquery": "node_modules/jquery/jquery",
"jquery-ui": "node_modules/jquery-ui-dist/jquery-ui",
"backbone": "node_modules/backbone/backbone",
"layoutmanager": "node_modules/layoutmanager/backbone.layoutmanager",
"backbone.subroute": "node_modules/backbone.subroute/backbone.subroute",
"bootstrap": "node_modules/bootstrap/dist/js/bootstrap",
"i18next": "node_modules/i18next-client/i18next.amd",
"text": "node_modules/requirejs-text/text",[...]
用于配置require.js的Config.js文件如下所示:
karma: {
options: {
basePath: "./",
singleRun: true,
captureTimeout: 7000,
autoWatch: true,
reporters: karmaOptions.reporters,
browsers: ["PhantomJS", "PhantomJS_custom"],
// you can define custom flags
customLaunchers: {
'PhantomJS_custom': {
base: 'PhantomJS',
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
},
},
flags: ['--load-images=true'],
debug: true
}
},
phantomjsLauncher: {
// Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
exitOnResourceError: true
},
files: [ "node_modules/karma-jasmine-async/src/jasmine.async.js", "vendor/bower/requirejs/require.js", "vendor/bower/underscore/underscore.js", "node_modules/phantomjs-prebuilt/bin/phantom.js", "test/runner.js",
{
pattern: "<%= rootFolderName %>/**/*.*",
included: false
},
{
pattern: "test/specs/*.*",
included: false
},
{
pattern: "test/mocks/*.*",
included: false
},
{
pattern: "vendor/**/*.js",
included: false
},
{
pattern: "vendor/**/*.html",
included: false
},
{
pattern: "vendor/**/*.xml",
included: false
}
],
exclude: [
'dist/**',
'vendor/**/*Spec.js'
]
},
// This creates a server that will automatically run your tests when you
// save a file and display results in the terminal.
daemon: {
options: {
singleRun: false
}
},
// This is useful for running the tests just once.
run: {
options: {
singleRun: true
}
},
run_functional: {
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
}
}
}
console.log("starting");
var webPage = require('webpage');
var page = webPage.create();
page.windowName = 'my-window';
page.settings.webSecurityEnabled = false;
page.open('http://google.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
debugger;
console.log("success");
}
phantom.exit();
});
require.config({
paths: {
"vendor": "vendor",
"node_modules": "node_modules",
"almond": "node_modules/almond/almond",
"underscore": "node_modules/lodash/dist/lodash.underscore",
"jquery": "node_modules/jquery/jquery",
"jquery-ui": "node_modules/jquery-ui-dist/jquery-ui",
"backbone": "node_modules/backbone/backbone",
"layoutmanager": "node_modules/layoutmanager/backbone.layoutmanager",
"backbone.subroute": "node_modules/backbone.subroute/backbone.subroute",
"bootstrap": "node_modules/bootstrap/dist/js/bootstrap",
"i18next": "node_modules/i18next-client/i18next.amd",
"text": "node_modules/requirejs-text/text",[...]
当我从命令行运行grunt测试时,我得到上面的错误:
错误:尚未加载上下文的模块名“网页”:。使用require([])
所以问题是,这个网页模块是phantom.js的一部分,它不是由require.js加载的。在test.js中
我应该在哪里装?在config.js中?我应该加载整个phantom.js还是只加载这个网页模块
我进行了重构,我的test.js现在是这样的:
var phantom = require('phantom');
phantom.create(function(ph){
ph.createPage(function(page) {
page.windowName = 'my-window';
page.settings.webSecurityEnabled = false;
page.open("http://www.google.com", function(status) {
page.render("google.pdf", function(){
console.log("page rendered");
ph.exit();
})
})
})
});
现在我得到:
PhantomJS 2.1.1(Mac OS X 0.0.0)错误
错误:尚未加载上下文的模块名“幻影”:。使用>要求([])
Require js应该从我附加的config.js中获取它:
"phantom": "node_modules/phantomjs-prebuilt/bin/phantom.js"
但由于某些原因,requirejs没有看到包含幻影js。
为什么会这样