gpt4 book ai didi

javascript - Webpack 不排除 node_modules

转载 作者:行者123 更新时间:2023-11-29 21:36:53 24 4
gpt4 key购买 nike

我有以下目录结构:


- webpack.config.js
- 来源
- 索引.js
-距离
- 索引.js

一个 webpack 配置:

module.exports = {
module: {
loaders: [{
test: /\.js$/,
include: __dirname + 'src',
exclude: __dirname + 'node_modules',
loader: "babel-loader"
}],
},
resolve: {
extensions: ['.js']
},
output: {
path: __dirname + '/dist',
filename: 'index.js'
}
};

但是如果我在我的 src/index.js 中导入请求(const request = require('request');),我会从 node_modules 中收到错误。

我怎样才能避免这种情况?我以为我已经排除了 node_modules。

只是为了完成我的 package.json:

{
"name": "leo-trans",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"watch": "webpack --watch",
"start": "node dist/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"preferGlobal": true,
"bin": {
"leo-trans": "./dist/index.js"
},
"devDependencies": {
"babel-core": "^6.3.26",
"babel-loader": "^6.2.1"
},
"dependencies": {
"cheerio": "^0.19.0",
"request": "^2.67.0"
}
}

最佳答案

您没有排除 node_modules,因为 __dirname 不包含尾部斜线。尝试使用 path.resolve :

const path = require('path');

module.exports = {
module: {
loaders: [{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
exclude: path.resolve(__dirname, 'node_modules'),
loader: "babel-loader"
}],
},
// ...
}

关于javascript - Webpack 不排除 node_modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34609437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com