2016-01-16 20 views
11

phép nói rằng theres một gói trong node_modules gọi foo và tôi muốn nhập một mô-đun trong một thư viện như foo/module qua webpack & babel ...Làm cách nào để nhập mô-đun trong thư mục con gói npm với gói webpack?

import Foo from 'foo'; làm việc

import SomeOtherModule from 'foo/module'; không thành công với những điều sau đây:

Module not found: Error: Cannot resolve module 'foo/module' in /Users/x/Desktop/someproject/js

Điều này làm cho webpack tìm kiếm tệp sai địa điểm thay vì node_modules

.210

webpack.config của tôi trông như thế này:

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

module.exports = { 
    entry: ['babel-polyfill','./js/script.js'], 
    output: { 
     path: __dirname, 
     filename: './build/script.js' 
    }, 
    module: { 
     loaders: [ 
      { 
       test: /\.js$/, 
       loader: 'babel', 
       query: { 
        cacheDirectory: true, 
        presets: ['es2015'] 
       } 
      } 
     ], 
    }, 
    plugins: [ 
     new webpack.NoErrorsPlugin() 
    ], 
    stats: { 
     colors: true 
    }, 
    devtool: 'source-map' 

}; 
+0

Hiển thị cấu trúc tệp và thư mục cho mô-đun foo, vui lòng –

Trả lời

7

Nó sẽ làm việc với import 'foo/module';. Nó sẽ giải quyết tập tin ./node_modules/foo/module.js hoặc ./node_modules/foo/module/index.jskhông một cái gì đó như ./node_modules/foo/node_modules/module/index.js nếu nó mong đợi (trong trường hợp đó bạn tốt hơn để cài đặt mô-đun qua npm).

+0

cấu trúc cho mô đun là './Node_modules/foo/module/index.js' không giải quyết được – glued

+0

Tôi đã kiểm tra dự án thử nghiệm và nó hoạt động khi tôi đổi tên ./ node_modules/foo/module/index.js đến ./node_modules/foo/module/index1.js, chạy webpack, nó nói "Không thể giải quyết module 'foo/module' trong/home/user/www/so-2/js" . Vì vậy, có thể bạn có typo trong tên tập tin? –

+0

oh bạn nói đúng, foo mô-đun có thư mục 'src' nên' nhập từ foo/src/module' đã hoạt động nhờ – glued

Các vấn đề liên quan