2017-07-22 190 views
0

我使用Sails 1與webpack,當我試圖從我的主要js文件導入jquery時,我得到這個錯誤大聲說,它不能解決jQuery的。webpack - 模塊未找到:錯誤:無法解析'jquery'

webpack.config.js:

var path = require('path'); 
var webpack = require('webpack'); 
var ExtractTextPlugin = require('extract-text-webpack-plugin'); 
var CleanWebpackPlugin = require('clean-webpack-plugin'); 
var CopyWebpackPlugin = require('copy-webpack-plugin'); 

module.exports.webpack = { 

    devtool: 'source-map', 
    entry: { 
     'admin': './assets/admin/js/admin.js' 
    }, 
    output: { 
     filename: 'js/[name].bundle.js', 
     path: path.resolve(__dirname, '..', '.tmp', 'public') 
    }, 
    resolve: { 
     modules: [ 
      path.join(__dirname, "js"), "node_modules" 
     ] 
    }, 
    module: { 
     // Extract less files 
     rules: [{ 
      test: /\.css$/, 
      loader: ExtractTextPlugin.extract({ use: 'css-loader' }) 
     }, { 
      test: /\.less$/, 
      loader: ExtractTextPlugin.extract({ use: 'css-loader!less-loader' }) 
     }, { 
      test: /\.js$/, 
      exclude: /node_modules/, 
      loader: 'babel-loader', 
      options: { 
       presets: [ 
        [ 'es2015', { modules: false } ] 
       ] 
      } 
     }] 
    }, 
    plugins: [ 

     new webpack.ProvidePlugin({ 
      $: 'jquery', 
      jQuery: 'jquery' 
     }), 
     // This plugin extracts CSS that was imported into .js files, and bundles 
     // it into separate .css files. The filename is based on the name of the 
     // .js file that the CSS was imported into. 
     new ExtractTextPlugin('css/[name].bundle.css'), 

     // This plugin cleans out your .tmp/public folder before lifting. 
     new CleanWebpackPlugin(['public'], { 
      root: path.resolve(__dirname, '..', '.tmp'), 
      verbose: true, 
      dry: false 
     }), 

     // This plugin copies the `images` and `fonts` folders into 
     // the .tmp/public folder. You can add any other static asset 
     // folders to this list and they'll be copied as well. 
     new CopyWebpackPlugin([{ 
      from: './assets/images', 
      to: path.resolve(__dirname, '..', '.tmp', 'public', 'images') 
     }]) 
    ] 
}; 

,並在我的資產/管理/ JS/admin.js我只有

import 'jquery'; 

jQuery是我的包JSON和它顯示在名爲jquery的node_modules包下。

另一個奇怪的是,如果我改變jQuery和嘗試導入,它完美的作品,所以我不認爲這是一個node_modules目錄問題,或者類似的東西。

+0

這有幫助嗎? https://stackoverflow.com/questions/43198547/webpack-module-not-found-error-cant-resolve-jquery –

+0

@jimcollins我看到了答案,但由於角度工作正常,並通過npm安裝就像jquery ,它似乎可能是不同的東西,我錯過了什麼? –

+1

配置看起來不錯,我不認爲它是與webpack相關的。也許模塊沒有正確安裝。你可以運行'node -e'require(「jquery」)?如果拋出一個錯誤,你應該刪除'node_modules'並重新安裝它們。 –

回答

0

一個不同的問題給我帶來了這裏。對於來自Google的未來訪問者,請嘗試運行:

webpack --display-error-details 
相關問題