問題
TypeError: fetch is not a function
at DigestClient.fetch (webpack-internal:///./node_modules/digest-fetch/digest-fetch-src.js:48:24)
at User.create (webpack-internal:///./node_modules/mongodb-atlas-api-client/src/user.js:53:26)
at Function.createOrgDBUser (webpack-internal:///./src/OrgUtils.ts:87:51)
at Function.createOrg (webpack-internal:///./src/apis/OrgAPI.ts:373:39)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async APIResponse.processHandlerFunction (webpack-internal:///./node_modules/apilove/lib/APIResponse.ts:27:31)
這就是我試圖用來[email protected]在集合上創建索引的錯誤。我在本地和 aws-lambda 中看到了這個錯誤。
atlasClient.user.create({...})
我使用 webpack 來捆綁我的 api,所以我認為問題在于我是如何捆綁的,但在我的研究中,我無法提出解決方案。
digest-fetch,由mongodb-atlas-api-client使用node-fetch,在沒有本機fetch功能的情況下使用。但是,我相信我的 webpack 配置加上digest-fetch匯入庫的方式是導致問題的原因。
以下代碼行來自node-modules/digest-fetch/digest-fetch-src.js:8
if (typeof(fetch) !== 'function' && canRequire) var fetch = require('node-fetch')
如果我將其更改為以下內容,則一切正常。換句話說,它匯入的模塊不是主要匯出的 fetch 函式node-fetch。
if (typeof(fetch) !== 'function' && canRequire) var fetch = require('node-fetch').default
描述node-fetch/package.json了三個入口點。
"main": "lib/index.js",
"browser": "./browser.js",
"module": "lib/index.mjs",
我認為正在發生的事情是我的 webpack 配置告訴 webpack 使用模塊入口點.mjs從node-fetch.export default fetch;
我的webpack.config.js
const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const NodemonPlugin = require('nodemon-webpack-plugin')
const ZipPlugin = require('zip-webpack-plugin')
module.exports = (env, argv) => {
const config = {
target: 'node',
watchOptions: {
ignored: ['node_modules', '*.js', '*.js.map', '*.d.ts'],
},
entry: './src/APIHandler.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: `APIHandler.js`,
libraryTarget: 'commonjs',
},
optimization: {
minimize: false,
// minimizer: [new TerserPlugin({})],
},
resolve: {
extensions: ['.ts', '.json', '.mjs', '.js'],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: {
loader: 'ts-loader',
options: {
transpileOnly: true,
allowTsInNodeModules: true,
},
},
},
],
},
plugins: [
new CleanWebpackPlugin(),
new CopyPlugin([
{ from: path.join(__dirname, 'src/certs'), to: path.resolve(__dirname, 'dist', 'certs') },
]),
new NodemonPlugin(),
],
}
// eslint-disable-next-line no-console
console.log({ mode: argv.mode })
if (argv.mode === 'development') {
config.devtool = 'eval-cheap-module-source-map'
}
if (argv.mode === 'production') {
config.plugins.push(
new ZipPlugin({
filename: 'handler',
})
)
}
return config
}
FWIW,這是我的 node_modules 中當前安裝的每個庫的版本。我在node v12.22.7本地使用。
"digest-fetch": "1.2.1",
"mongodb-atlas-api-client": "2.31.0",
"node-fetch": "2.6.7",
"webpack": "4.46.0"
問題
我錯過了什么?我需要對我進行什么更改才能webpack.config.js正確決議到主模塊匯出node-fetch?
注意:在我的研究中,我發現其他人有這個問題,但沒有任何解決方案對我有幫助。
uj5u.com熱心網友回復:
解決方案非常簡單。我添加mainFields到 webpack 配置的resolve屬性中。
resolve: {
extensions: ['.ts', '.json', '.mjs', '.js'],
mainFields: ['main', 'module'],
}
這告訴 webpack 首先使用main模塊的屬性,如果沒有找到package.json則回退到該屬性。module
有關更多資訊,請參閱webpack 的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464205.html
標籤:javascript 网页包 节点获取
