gpt4 book ai didi

angularjs - 将 Webpack 与 Karma 一起使用会导致 Node 模块加载错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:10:02 24 4
gpt4 key购买 nike

我正在尝试使用 Karma 运行我的 Jasmine 测试,我相信一些应该加载的模块没有加载,这可能是一个依赖问题,但我已经没有想法了。

karma.conf.js

var webpackConfig = require('./webpack.config.js');
webpackConfig.entry = {};

module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
{ pattern: 'test-context.js'}
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test-context.js': ['webpack']
},
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
debug: true
})
}

webpack.config.js

var path = require('path');
var webpack = require('webpack');

module.exports = {
entry: [
'babel-polyfill',
'./index.js'
],
output: {
publicPath: '/',
filename: 'main.js'
},
devtool: 'source-map',
resolve: {
extensions: ['', '.js', '.jsx']
},
module: {
rules: [
{
test: /\.json$/,
use: 'json-loader'
}
],
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.json$/,
loader: 'json-loader'
}
]
},
node: {
net: 'empty',
tls: 'empty',
//dns: 'empty',
fs: 'empty'
},
externals: {
//'crypto': 'crypto'
},
debug: true
};

package.json

{
"name": "Web-Application",
"version": "0.0.1",
"description": "A web application",
"engines": {
"node": "5.9.1"
},
"main": "index.js",
"scripts": {
"start": "node index.js",
"tests": "karma start"
},
"dependencies": {
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.23.0",
"babel-register": "^6.23.0",
"box-node-sdk": "^1.3.0",
"ejs": "2.4.1",
"express": "4.13.3",
"node-libs-browser": "^2.0.0",
"crypto-browserify": "^3.11.0"
},
"repository": {
"type": "git",
"url": "https://github.com/heroku/node-js-getting-started"
},
"keywords": [
"node",
"heroku",
"express"
],
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-core": "^6.23.1",
"babel-loader": "^v6.4.0",
"babel-preset-env": "^1.2.0",
"babel-preset-es2015": "^6.22.0",
"express": "4.13.3",
"jasmine-core": "^2.5.2",
"json-loader": "^0.5.4",
"karma": "^1.5.0",
"karma-babel-preprocessor": "^6.0.1",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.1.0",
"karma-phantomjs2-launcher": "^0.5.0",
"karma-webpack": "^2.0.2",
"webpack": "^1.3.0",
"webpack-dev-server": "1.10.1"
}
}

我添加

node: {
net: 'empty',
tls: 'empty',
//dns: 'empty',
fs: 'empty'
},

进入代码以消除以前的依赖问题,但这可能只是修复了服务层,并且可能存在更深层次的依赖问题。

堆栈跟踪:

enter image description here

enter image description here

Git 源代码

https://github.com/noobiehacker/revaBoxWeb

感谢任何帮助或提示><

最佳答案

此示例是一个使用 express 的 Node.js 应用程序,这需要 Node.js 内置模块,例如 fsnet。但是您尝试使用 webpack 构建一个 Web 应用程序(默认目标是 web),并且在浏览器中这些模块不可用。 Webpack 告诉您无法解析模块,但您决定清空它们,这会给您带来运行时错误。

为了将 express 与 webpack 一起使用,您需要配置 targetnode,因此 webpack 不会触及内置模块。

target: 'node'

您还需要删除之前添加的node配置。

I am trying to just run my Jasmine Test with Karma

如上所述,您正在构建一个 Node.js 应用程序,但是 Karma是一个浏览器测试工具。它将无法运行您的代码。您的代码应该由 Node 运行并由 Node (而不是浏览器)测试。

关于angularjs - 将 Webpack 与 Karma 一起使用会导致 Node 模块加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42658659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com