我正在使用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
似乎有点问题。我曾经有这个包,但我只是删除了它。
答案 0 :(得分:-1)
您可能缺少包裹。
只需尝试安装软件包fs.promises
即可解决您的问题。