【问题标题】:How do I import a module from a different `/node_modules/` folder如何从不同的 `/node_modules/` 文件夹导入模块
【发布时间】:2019-02-11 12:10:19
【问题描述】:

这是我的文件结构:

├── public/
│   ├── assets
│   │   ├── node_modules
│   │   │   ├── jquery
│   │   │   ├── etc... 
│   │   ├── images
│   │   ├──  icons
│   │   ├──  package.json
│   ├── es
│   │   ├── index.js
│   ├── js
│   │   ├── bundle.js
│   ├── scss
│   ├── css
│   ├── index.php
├── app/
│   ├── contollers
│   ├── models
│   ├── views
│   ├── helpers
├── node_modules/
│   ├── webpack/
│   │   ├── ... 
│   │   │   ├── ...
├── package.json
└── webpack.conf.js/

我创建了 2 个单独的 npms,一个用于根文件夹 (/www) 用于开发,另一个用于 public/assets/ 下的资产(图像、图标和节点模块)。

我想将 jquery 库(例如)模块加载到我的 index.js 文件中。

试过了:

import '../scss/style.scss';

import {$,jQuery} from '../assets/node_models/jquery';

返回

./public/es/index.js 中的错误模块未找到:错误:无法解析 '../assets/node_models/jquery' 在'/www/public/es' @ ./public/es/index.js 5:14-53

这是我的 webpack.conf.js 文件:

const path              = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UrlLoader         = require('url-loader');

module.exports = {

    entry: { 
                main: './public/es/index.js' 
    },
    output: {
                path:       path.resolve(__dirname, 'public/js/'),
                filename:   'bundle.js'
    },
    module: {
                rules: [
                            {
                                test:       /\.js$/,
                                exclude:    /node_modules/,
                                use: {
                                        loader: "babel-loader"
                                }
                            }, 
                            {
                                test: /\.s?css$/,
                                use: ExtractTextPlugin.extract({
                                    fallback: 'style-loader',
                                        use: ['css-loader', 'sass-loader']
                                })
                            }, 
                            {
                                test: /\.(png|gif|jpe|jpg|woff|woff2|eot|ttf|svg)(\?.*$|$)/,
                                use: [{
                                    loader: 'url-loader',
                                    options: {
                                      limit: 100000
                                    }
                                }]
                            }
                ]
    },
    plugins: [
                new ExtractTextPlugin({
                    filename: '../css/main-style.css' 
                }), 
                new HtmlWebpackPlugin({
                    inject: false,
                    hash: true,
                    template: './public/index.php',
                    filename: 'index.php'
                })
    ],
    devServer: {
                    port:           3000,
                    contentBase:    __dirname + '/public/js', 
                    inline:         true
    }

};

如何从其他文件夹加载模块?

【问题讨论】:

    标签: npm webpack ecmascript-6 node-modules es6-modules


    【解决方案1】:

    你必须像这样导入 jquery: import $ from 'assets/node_modules/jquery';

    你也可以稍微改进一下你的 webpack:

    const path              = require('path');
    const ExtractTextPlugin = require('extract-text-webpack-plugin');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const UrlLoader         = require('url-loader');
    
    
    publicFolder = path.resolve(__dirname, 'public');
    appFolder = path.resolve(__dirname, 'app');
    
    module.exports = {
    
        resolve: {
            modules: [ publicFolder, appFolder ],
            extensions: [ '.js', ],
        },
    
        entry: { 
                    main: './public/es/index.js' 
        },
        output: {
                    path:       path.resolve(__dirname, 'public/js/'),
                    filename:   'bundle.js'
        },
        module: {
                    rules: [
                                {
                                    test:       /\.js$/,
                                    exclude:    /node_modules/,
                                    use: {
                                            loader: "babel-loader"
                                    }
                                }, 
                                {
                                    test: /\.s?css$/,
                                    use: ExtractTextPlugin.extract({
                                        fallback: 'style-loader',
                                            use: ['css-loader', 'sass-loader']
                                    })
                                }, 
                                {
                                    test: /\.(png|gif|jpe|jpg|woff|woff2|eot|ttf|svg)(\?.*$|$)/,
                                    use: [{
                                        loader: 'url-loader',
                                        options: {
                                          limit: 100000
                                        }
                                    }]
                                }
                    ]
        },
        plugins: [
                    new ExtractTextPlugin({
                        filename: '../css/main-style.css' 
                    }), 
                    new HtmlWebpackPlugin({
                        inject: false,
                        hash: true,
                        template: './public/index.php',
                        filename: 'index.php'
                    })
        ],
        devServer: {
                        port:           3000,
                        contentBase:    __dirname + '/public/js', 
                        inline:         true
        }
    
    };
    

    我已经添加了

    publicFolder = path.resolve(__dirname, 'public');
    appFolder = path.resolve(__dirname, 'app');
    

    resolve: {
        modules: [ publicFolder, appFolder ],
        extensions: [ '.js', ],
    },
    

    之后,您可以从 publicFolderappFolder 导入任何东西,就像从 node_modules 一样。例如

    import {$,jQuery} from 'assets/node_models/jquery';
    

    【讨论】:

    • 我添加了 2 个新变量。使用您提到的设置添加解决方案并更改了我的导入语句:`ERROR in ./public/es/index.js Module not found: Error: Can't resolve 'assets/node_models/jquery' in '/www/public/es ' @ ./public/es/index.js 5:14-50
    • webpack 配置没问题。问题是jquery 不要为您提供$ jquery,因此您无法导入它们。安装 loadash 并尝试 import _ from 'assets/node_modules/lodash'; 你会看到它按预期工作。
    • 我尝试在 /www 下安装 jquery 只是为了运动,它确实使用 $jquery... 但确实尝试用你的例子来测试它:错误./public/es/index.js 未找到模块:错误:无法解析“/www/public/es”中的“assets/node_modules/lodash”@ ./public/es/index.js 5:14-51
    • 我控制台记录了路径,这些是正确的值吗? /www/public/www/app
    • 非常喜欢斯帕西巴:D
    猜你喜欢
    • 2018-08-22
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多