使用karma-webpack时找不到模块错误 [英] Cannot find module error when using karma-webpack

查看:279
本文介绍了使用karma-webpack时找不到模块错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下karma.conf.js文件,我还有一个名为example.spec.js的测试文件,该文件位于app/compontents/example/example.spec.js内部,我正在尝试导入我的Example.js file

I have the following karma.conf.js file, I also have a test file called example.spec.js that lives inside app/compontents/example/example.spec.js I am trying to import my Example.js file

import Example from 'app/components/Example/Example.jsx';

describe('Example Component', function() {

});

但是我遇到以下错误Error: Cannot find module "app/components/Example/Example.jsx"我尝试了许多不同的想法,例如./Example.jsx./Examplecomponents/Example/Example.jsx,但是每次遇到相同的错误.

But I get the following error Error: Cannot find module "app/components/Example/Example.jsx" I have tried many different ideas, such as ./Example.jsx, ./Example, components/Example/Example.jsx but each time I get the same error.

var webpack = require('webpack');

module.exports = function(config) {
 config.set({

// milliseconds
browserNoActivityTimeout: 40000,

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'chai'],


// list of files / patterns to load in the browser
files: [
  'app/tests/setup.js',
  'app/**/*.spec.js'
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
  'app/tests/setup.js': ['webpack', 'sourcemap'],
  'app/**/*.spec.js': ['webpack', 'sourcemap']
},


webpack: {
  devtool: 'inline-source-map',
  cache: false,
  resolve: {
    extensions: ['', '.js', '.jsx'],
    modulesDirectories: ['node_modules', 'app'],
    fallback: __dirname
  },
  module: {
    preLoaders: [],
    loaders: [
      { test: /\.(js|jsx)$/, loaders: ['babel-loader'], exclude: /node_modules/ },
    ],
    plugins: [
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify('test')
      })
    ]
  }
},


webpackMiddleware: {
  progress: false,
  stats: false,
  debug: false,
  noInfo: true,
  silent: true
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['dots'],


// 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: false,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,

plugins: [
  'karma-mocha',
  'karma-chai',
  'karma-webpack',
  'karma-sourcemap-loader',
  'karma-phantomjs-launcher'
]
});
};

推荐答案

如果要使用这样的导入,则需要设置

If you want to use an import like that, you'll need to set up a resolve.alias. Demo:

resolve: {
    alias: {
        app: path.join(__dirname, 'app')
    }
}

这篇关于使用karma-webpack时找不到模块错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆