这是我的文件结构:
如何从其他文件夹加载模块?
├── 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/
我创建了两个不同的npm,一个用于根目录 (/www
) 用于开发,另一个用于资源(图片、图标和node_modules)在public/assets/
下。
我想将jQuery库(例如)模块加载到我的index.js
文件中。
尝试过:
import '../scss/style.scss';
import {$,jQuery} from '../assets/node_models/jquery';
返回结果:
在webpack.conf.js文件中,我遇到了以下错误:
错误信息: 在路径“/www/public/es”中找不到“../assets/node_models/jquery”文件。
错误位置: ./public/es/index.js 的第5行和第14-53字符
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
}
};
如何从其他文件夹加载模块?
jquery
没有为您提供$
jquery
,因此您无法导入它们。安装lodash
并尝试import _ from 'assets/node_modules/lodash';
,您会看到它按预期工作。 - Arseniy-II/www/public
/www/app
- Imnotapotato