javascript 未捕获的 ReferenceError:需要未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14111785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught ReferenceError: require is not defined
提问by pgreen2
For a project that I am working on I have been using a hodgepodge of JavaScript libraries. The main logic of my code is broken down into multiple commonjs modules. I use google closure to combine the modules into one output js file which I use within my AngularJS application.
对于我正在从事的项目,我一直在使用 JavaScript 库的大杂烩。我的代码的主要逻辑被分解为多个 commonjs 模块。我使用 google 闭包将模块组合成一个输出 js 文件,我在我的 AngularJS 应用程序中使用该文件。
The problem I am having is trying to perform tests with testacular. There error I receive is Uncaught ReferenceError: require is not defined
. It is happening because, unlike google closure, testacular doesn't understand commonjs modules. There are a couple work arounds I can do, but I was hoping to make it work without having to restructure my code.
我遇到的问题是试图用睾丸进行测试。我收到的错误是Uncaught ReferenceError: require is not defined
. 之所以发生这种情况,是因为与 google 关闭不同,testacular 不理解 commonjs 模块。我可以做一些变通方法,但我希望无需重构我的代码就能让它工作。
- I can restucture the modules so that I'm no longer using commonjs. I don't like this because it feels like a step backwards. I want my code to be modular.
- I can run testacular on the compiled js from google closure. I don't mind doing it this way, but I have not been able to trigger everything to work on file changes. Testacular can re-run itself on file changes, but I haven't seen anyway to make google closure re-compile on changes.
- Finally, I can enable commonjs module in testacular. Ideally this is the way I want to go, but it may not be the easiest.
- 我可以重新构造模块,这样我就不再使用 commonjs。我不喜欢这样,因为感觉像是倒退了一步。我希望我的代码是模块化的。
- 我可以在 google 关闭的编译后的 js 上运行 testacular。我不介意这样做,但是我无法触发所有内容来处理文件更改。Testacular 可以在文件更改时重新运行,但我还没有看到让 google 关闭重新编译更改。
- 最后,我可以在 testacular 中启用 commonjs 模块。理想情况下,这是我想要的方式,但它可能不是最简单的。
Has anyone else run into a similar problem? I'm open for trying different things; I just don't want anything hacky.
有没有其他人遇到过类似的问题?我愿意尝试不同的事情;我只是不想要任何hacky。
javaclassstreamreader.spec.js:
javaclassstreamreader.spec.js:
"use strict"
var JavaClassStreamReader = require('../javaclassstreamreader.js').JavaClassStreamReader;
describe('javaclassstreamreader', function() {
it('reader can be constructed', function() {
var dataView = {
byteLength : 0
};
//FIXME load dataView
var reader = new JavaClassStreamReader(dataView);
expect(reader.dataView).toBe(dataView);
expect(reader.offset).toBe(0);
expect(reader.maxOffset).toBe(0);
});
});
javaclassstreamreader.js:
javaclassstreamreader.js:
function JavaClassStreamReader(dataView, initialOffset, maxBytesToRead) {
this.dataView = dataView;
this.offset = initialOffset || 0;
this.maxOffset = this.offset + (maxBytesToRead || this.dataView.byteLength);
}
//... code trucated ...
采纳答案by pgreen2
I was not able to make it work with require
, but I do have a partial solution.
我无法使其与 一起使用require
,但我确实有部分解决方案。
grunt.js:
grunt.js:
/*global module:false*/
module.exports = function(grunt) {"use strict";
// Project configuration.
grunt.initConfig({
pkg : '<json:package.json>',
meta : {
banner : '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' + '<%= grunt.template.today("yyyy-mm-dd") %>\n' + '<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' + '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' + ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
},
lint : {
files : ['grunt.js', 'src/*.js', 'src/public/js/**/*.js', 'src/specs/**/*.js']
},
watch : {
files : '<config:lint.files>',
tasks : 'default'
},
exec : {
ensure_generated_directory : {
command : 'mkdir -p generated/js/'
}
},
clean : {
all : ['generated']
},
jshint : {
files : '<config:lint.files>',
options : {
curly : true,
eqeqeq : true,
forin : true,
immed : true,
latedef : true,
newcap : true,
noarg : true,
sub : true,
undef : true,
unused : true,
strict : true,
boss : true,
eqnull : true,
es5 : true,
browser : true,
jquery : true,
devel : true
},
globals : {
//jasmine
describe : false,
it : false,
expect : false,
//commonjs
require : false,
exports : true,
//angular
angular : false
}
},
'closure-compiler' : {
frontend : {
closurePath : 'closure-compiler',
js : ['src/*.js', 'src/public/js/**/*.js'],
jsOutputFile : 'generated/js/complete-app.js',
options : {
externs : 'externs.js',
compilation_level : 'SIMPLE_OPTIMIZATIONS',
language_in : 'ECMASCRIPT5_STRICT',
logging_level : 'ALL',
debug : null,
warning_level : 'verbose',
summary_detail_level : 3,
formatting : ['PRETTY_PRINT', 'PRINT_INPUT_DELIMITER'],
common_js_entry_module : 'src/public/js/app.js',
process_common_js_modules : null,
process_jquery_primitives : null,
common_js_module_path_prefix : 'src'
}
}
},
testacularServer : {
integration : {
options : {
keepalive : true
},
configFile : 'testacular.conf.js',
autoWatch : false,
singleRun : true
}
}
});
// Default task.
grunt.registerTask('default', 'lint exec:ensure_generated_directory closure-compiler testacularServer:integration');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-closure-compiler');
grunt.loadNpmTasks('grunt-exec');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-testacular');
};
I can run grunt watch
and I get a similar result. grunt lints, then compiles, then runs testacular. This isn't as fast as I was hoping. testacular starts and stops the server each time.
我可以运行grunt watch
并得到类似的结果。grunt lints,然后编译,然后运行 testacular。这并不像我希望的那么快。testacular 每次启动和停止服务器。
回答by asgoth
It seems there is/was an issuewith Testacular.
Testacular似乎存在/曾经存在问题。
Could you try the following:
您可以尝试以下操作:
- clear npm cache:
npm cache clean
- install another version of testacular:
npm install -g [email protected]
- 清除 npm 缓存:
npm cache clean
- 安装另一个版本的 testacular:
npm install -g [email protected]