【发布时间】:2020-11-27 12:47:45
【问题描述】:
我正在使用 React 构建一个 Web 应用程序。我从来没有在文件中调用fs,一切正常,直到我突然收到这个错误:
Uncaught Error: Cannot find module 'fs'
然后我用谷歌搜索了如何解决这个问题,我找到了this answer。 遵循此答案时,我会收到此错误:
Uncaught ReferenceError: require is not defined
Making fs an empty object raises the same error.
有人知道如何解决这个问题吗?
编辑
第一个错误的完整输出:
main.js:24 Uncaught Error: Cannot find module 'fs'
at webpackMissingModule (main.js:24)
at Object.eval (main.js:24)
at eval (main.js:115)
at Object../node_modules/dotenv/lib/main.js (bundle.js:73895)
at __webpack_require__ (bundle.js:20)
at eval (index.tsx:6)
at Module../src/index.tsx (bundle.js:76423)
at __webpack_require__ (bundle.js:20)
at bundle.js:84
at bundle.js:87
这里是bundle.js:73895:
eval("/* WEBPACK VAR INJECTION */(function(process) {/* @flow */\n/*::\n\ntype DotenvParseOptions = {\n debug?: boolean\n}\n\n// keys and values from src\ntype DotenvParseOutput = { [string]: string }\n\ntype DotenvConfigOptions = {\n path?: string, // path to .env file\n encoding?: string, // encoding of .env file\n debug?: string // turn on logging for debugging purposes\n}\n\ntype DotenvConfigOutput = {\n parsed?: DotenvParseOutput,\n error?: Error\n}\n\n*/\n\nconst fs = __webpack_require__(!(function webpackMissingModule() { var e = new Error(\"Cannot find module 'fs'\"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))\nconst path = __webpack_require__(/*! path */ \"./node_modules/path-browserify/index.js\")\n\nfunction log (message /*: string */) {\n console.log(`[dotenv][DEBUG] ${message}`)\n}\n\nconst NEWLINE = '\\n'\nconst RE_INI_KEY_VAL = /^\\s*([\\w.-]+)\\s*=\\s*(.*)?\\s*$/\nconst RE_NEWLINES = /\\\\n/g\nconst NEWLINES_MATCH = /\\n|\\r|\\r\\n/\n\n// Parses src into an Object\nfunction parse (src /*: string | Buffer */, options /*: ?DotenvParseOptions */) /*: DotenvParseOutput */ {\n const debug = Boolean(options && options.debug)\n const obj = {}\n\n // convert Buffers before splitting into lines and processing\n src.toString().split(NEWLINES_MATCH).forEach(function (line, idx) {\n // matching \"KEY' and 'VAL' in 'KEY=VAL'\n const keyValueArr = line.match(RE_INI_KEY_VAL)\n // matched?\n if (keyValueArr != null) {\n const key = keyValueArr[1]\n // default undefined or missing values to empty string\n let val = (keyValueArr[2] || '')\n const end = val.length - 1\n const isDoubleQuoted = val[0] === '\"' && val[end] === '\"'\n const isSingleQuoted = val[0] === \"'\" && val[end] === \"'\"\n\n // if single or double quoted, remove quotes\n if (isSingleQuoted || isDoubleQuoted) {\n val = val.substring(1, end)\n\n // if double quoted, expand newlines\n if (isDoubleQuoted) {\n val = val.replace(RE_NEWLINES, NEWLINE)\n }\n } else {\n // remove surrounding whitespace\n val = val.trim()\n }\n\n obj[key] = val\n } else if (debug) {\n log(`did not match key and value when parsing line ${idx + 1}: ${line}`)\n }\n })\n\n return obj\n}\n\n// Populates process.env from .env file\nfunction config (options /*: ?DotenvConfigOptions */) /*: DotenvConfigOutput */ {\n let dotenvPath = path.resolve(process.cwd(), '.env')\n let encoding /*: string */ = 'utf8'\n let debug = false\n\n if (options) {\n if (options.path != null) {\n dotenvPath = options.path\n }\n if (options.encoding != null) {\n encoding = options.encoding\n }\n if (options.debug != null) {\n debug = true\n }\n }\n\n try {\n // specifying an encoding returns a string instead of a buffer\n const parsed = parse(fs.readFileSync(dotenvPath, { encoding }), { debug })\n\n Object.keys(parsed).forEach(function (key) {\n if (!Object.prototype.hasOwnProperty.call(process.env, key)) {\n process.env[key] = parsed[key]\n } else if (debug) {\n log(`\"${key}\" is already defined in \\`process.env\\` and will not be overwritten`)\n }\n })\n\n return { parsed }\n } catch (e) {\n return { error: e }\n }\n}\n\nmodule.exports.config = config\nmodule.exports.parse = parse\n\n/* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../process/browser.js */ \"./node_modules/process/browser.js\")))\n\n//# sourceURL=webpack:///./node_modules/dotenv/lib/main.js?");
这似乎与dotenv 有关。我以前有这个包,但我刚刚删除了它。
【问题讨论】:
-
看起来你的 build/babel/webpack 有问题。
-
它是否提供任何指示 fs 模块正在使用的位置?
-
看起来您现在正在为节点设置目标,它将使用导致问题的节点 api。你能分享你的 webpack 配置吗?
-
Object../node_modules/dotenv/lib/main.js— 可能你需要从你的包中排除它
标签: javascript reactjs webpack