Module not found: Error: Can't resolve 'fs' in

Oke, I upgraded Angular from 6 to 8. But I stil get errors.

I found on internet a solution that for a lot of users helped. But in this case it doesn't helped me.

So my package.json file looks like this:

{
"name": "vital10-frontend",
"version": "0.55.0",
"license": "Unlicensed",
"scripts": {
"ng": "ng",
"start": "ng serve",
"hmr": "ng serve --configuration hmr",
"build": "ng build",
"build:prod": "npm run sass:prod && npm run vit10prod",
"build:acc": "npm run sass:prod && npm run vit10acc",
"build:test": "npm run sass:prod && npm run vit10test",
"build:dev": "npm run sass:prod && npm run vit10dev",
"test": "ng test",
"test:cover": "ng test --code-coverage",
"lint": "ng lint",
"e2e": "ng e2e",
"local": "ng serve -c local",
"analyze-bundle": "webpack-bundle-analyzer dist/stats.json",
"postinstall": "node patch-webpack.js",
"sass:watch": "sass --watch sass:src\\assets",
"sass:prod": "sass --update -f --style compressed --sourcemap=none sass:src\\assets",
"vit10dev": "ng build --prod -c vit10dev",
"vit10test": "ng build --prod -c vit10test",
"vit10acc": "ng build --prod -c vit10acc",
"vit10prod": "ng build --prod -c production"
},
"private": true,
"dependencies": {
"@angular/animations": "^8.1.2",
"@angular/cdk": "^7.3.7",
"@angular/common": "^8.1.2",
"@angular/compiler": "^8.1.2",
"@angular/core": "^8.1.2",
"@angular/forms": "^8.1.2",
"@angular/http": "^7.2.15",
"@angular/platform-browser": "^8.1.2",
"@angular/platform-browser-dynamic": "^8.1.2",
"@angular/platform-server": "^8.1.2",
"@angular/router": "^8.1.2",
"@types/chart.js": "^2.7.54",
"buffer": "^5.2.1",
"chart.js": "^2.7.2",
"core-js": "^2.5.7",
"events": "^3.0.0",
"fs": "0.0.1-security",
"jasmine": "^3.1.0",
"ng2-charts": "^1.6.0",
"ng2-dragula": "^2.1.1",
"ng2-pdf-viewer": "^5.0.1",
"node-sass": "^4.11.0",
"oidc-client": "^1.5.1",
"path": "^0.12.7",
"pdfjs-dist": "^2.0.489",
"rxjs": "^6.5.2",
"rxjs-compat": "^6.5.2",
"stream": "0.0.2",
"timers": "^0.1.1",
"web-animations-js": "^2.3.1",
"zone.js": "^0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.801.2",
"@angular/cli": "^8.1.2",
"@angular/compiler-cli": "^8.1.2",
"@angular/language-service": "^8.1.2",
"@angularclass/hmr": "^2.1.3",
"@compodoc/compodoc": "^1.1.10",
"@types/jasmine": "^3.3.14",
"@types/node": "^12.6.8",
"chai": "^4.1.2",
"codelyzer": "^5.1.0",
"convert-csv-to-json": "0.0.13",
"cucumber": "^4.2.1",
"cucumber-html-reporter": "^5.0.0",
"cucumber-junit": "^1.7.1",
"express-static": "^1.2.5",
"jasmine-core": "^3.4.0",
"jasmine-reporters": "^2.3.1",
"jasmine-spec-reporter": "^4.2.1",
"karma": "^4.2.0",
"karma-chrome-launcher": "^3.0.0",
"karma-cli": "^2.0.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-html-reporter": "^0.2.7",
"karma-jasmine": "^2.0.1",
"karma-jasmine-html-reporter": "^1.4.2",
"karma-junit-reporter": "^1.2.0",
"karma-teamcity-reporter": "^1.1.0",
"moment": "^2.22.1",
"prettier": "1.13.4",
"protractor": "^5.4.2",
"protractor-beautiful-reporter": "^1.2.1",
"protractor-cucumber-framework": "^5.0.0",
"puppeteer": "^1.14.0",
"request": "^2.87.0",
"request-promise-native": "^1.0.5",
"ts-node": "^3.3.0",
"tslint": "^5.18.0",
"tslint-angular": "^1.1.2",
"typescript": "3.4.5",
"webpack-bundle-analyzer": "^3.3.2",
"xlsx": "^0.12.13"
},
"browser": {
"fs": false,
"path": false,
"os": false,
"crypto": false,
"stream": false,
"http": false,
"tls": false,
"zlib": false,
"https": false,
"net": false
}
}

And my b tsconfig.json file looks like this:

{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
]
},
"angularCompilerOptions": {
"enableIvy": false
}
}

But I still get these errors:

ERROR in ./node_modules/resolve/lib/async.js
Module not found: Error: Can't resolve 'fs' in 'C:\Source\nien\vital10-frontend\node_modules\resolve\lib'
ERROR in ./node_modules/resolve/lib/sync.js
Module not found: Error: Can't resolve 'fs' in 'C:\Source\nien\vital10-frontend\node_modules\resolve\lib'
ERROR in ./node_modules/resolve/lib/node-modules-paths.js
Module not found: Error: Can't resolve 'fs' in 'C:\Source\nien\vital10-frontend\node_modules\resolve\lib'
ERROR in ./node_modules/tslint/lib/utils.js
Module not found: Error: Can't resolve 'fs' in 'C:\Source\nien\vital10-frontend\node_modules\tslint\lib'

I found a possible solution, that you have to put some configuration in one of the node_modules. But I think that is not a good solution. Because if you remove all the imports and then install again the imports your configurations are gone.

Thank you

Oke, I removed the

 "fs": "0.0.1-security",

I deleted all the packes(node_modules) and did a npm install.

But now I get this:

(node:13880) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added. Use emitter.setMaxListeners() to increase limit
Binary found at C:\Source\nien\vital10-frontend\node_modules\node-sass\vendor\win32-x64-64\binding.node
Testing binary
Binary is fine


> vital10-frontend@0.55.0 postinstall C:\Source\nien\vital10-frontend
> node patch-webpack.js


internal/modules/cjs/loader.js:638
throw err;
^


Error: Cannot find module 'C:\Source\nien\vital10-frontend\patch-webpack.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! vital10-frontend@0.55.0 postinstall: `node patch-webpack.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the vital10-frontend@0.55.0 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.


npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\nien\AppData\Roaming\npm-cache\_logs\2019-07-23T10_25_46_184Z-debug.log
PS C:\Source\nien\vital10-frontend>

and my package.json file looks like this now:

{
"name": "vital10-frontend",
"version": "0.55.0",
"license": "Unlicensed",
"scripts": {
"ng": "ng",
"start": "ng serve",
"hmr": "ng serve --configuration hmr",
"build": "ng build",
"build:prod": "npm run sass:prod && npm run vit10prod",
"build:acc": "npm run sass:prod && npm run vit10acc",
"build:test": "npm run sass:prod && npm run vit10test",
"build:dev": "npm run sass:prod && npm run vit10dev",
"test": "ng test",
"test:cover": "ng test --code-coverage",
"lint": "ng lint",
"e2e": "ng e2e",
"local": "ng serve -c local",
"analyze-bundle": "webpack-bundle-analyzer dist/stats.json",
"postinstall": "node patch-webpack.js",
"sass:watch": "sass --watch sass:src\\assets",
"sass:prod": "sass --update -f --style compressed --sourcemap=none sass:src\\assets",
"vit10dev": "ng build --prod -c vit10dev",
"vit10test": "ng build --prod -c vit10test",
"vit10acc": "ng build --prod -c vit10acc",
"vit10prod": "ng build --prod -c production"
},
"private": true,
"dependencies": {
"@angular/animations": "^8.1.2",
"@angular/cdk": "^7.3.7",
"@angular/common": "^8.1.2",
"@angular/compiler": "^8.1.2",
"@angular/core": "^8.1.2",
"@angular/forms": "^8.1.2",
"@angular/http": "^7.2.15",
"@angular/platform-browser": "^8.1.2",
"@angular/platform-browser-dynamic": "^8.1.2",
"@angular/platform-server": "^8.1.2",
"@angular/router": "^8.1.2",
"@types/chart.js": "^2.7.54",
"buffer": "^5.2.1",
"chart.js": "^2.7.2",
"core-js": "^2.5.7",
"events": "^3.0.0",
"jasmine": "^3.1.0",
"ng2-charts": "^1.6.0",
"ng2-dragula": "^2.1.1",
"ng2-pdf-viewer": "^5.0.1",
"node-sass": "^4.11.0",
"oidc-client": "^1.5.1",
"path": "^0.12.7",
"pdfjs-dist": "^2.0.489",
"rxjs": "^6.5.2",
"rxjs-compat": "^6.5.2",
"stream": "0.0.2",
"timers": "^0.1.1",
"web-animations-js": "^2.3.1",
"zone.js": "^0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.801.2",
"@angular/cli": "^8.1.2",
"@angular/compiler-cli": "^8.1.2",
"@angular/language-service": "^8.1.2",
"@angularclass/hmr": "^2.1.3",
"@compodoc/compodoc": "^1.1.10",
"@types/jasmine": "^3.3.14",
"@types/node": "^12.6.8",
"chai": "^4.1.2",
"codelyzer": "^5.1.0",
"convert-csv-to-json": "0.0.13",
"cucumber": "^4.2.1",
"cucumber-html-reporter": "^5.0.0",
"cucumber-junit": "^1.7.1",
"express-static": "^1.2.5",
"jasmine-core": "^3.4.0",
"jasmine-reporters": "^2.3.1",
"jasmine-spec-reporter": "^4.2.1",
"karma": "^4.2.0",
"karma-chrome-launcher": "^3.0.0",
"karma-cli": "^2.0.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-html-reporter": "^0.2.7",
"karma-jasmine": "^2.0.1",
"karma-jasmine-html-reporter": "^1.4.2",
"karma-junit-reporter": "^1.2.0",
"karma-teamcity-reporter": "^1.1.0",
"moment": "^2.22.1",
"prettier": "1.13.4",
"protractor": "^5.4.2",
"protractor-beautiful-reporter": "^1.2.1",
"protractor-cucumber-framework": "^5.0.0",
"puppeteer": "^1.14.0",
"request": "^2.87.0",
"request-promise-native": "^1.0.5",
"ts-node": "^3.3.0",
"tslint": "^5.18.0",
"tslint-angular": "^1.1.2",
"typescript": "3.4.5",
"webpack-bundle-analyzer": "^3.3.2",
"xlsx": "^0.12.13"
},
"browser": {
"fs": false,
"path": false,
"os": false,
"crypto": false,
"stream": false,
"http": false,
"tls": false,
"zlib": false,
"https": false,
"net": false
}
}

Still get this errrors:

(node:15956) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added. Use emitter.setMaxListeners() to increase limit
internal/modules/cjs/loader.js:638
throw err;
^


Error: Cannot find module 'C:\Source\nien\vital10-frontend\patch-webpack.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! vital10-frontend@0.55.0 postinstall: `node patch-webpack.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the vital10-frontend@0.55.0 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.


npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\nien\AppData\Roaming\npm-cache\_logs\2019-07-23T10_59_04_891Z-debug.log
PS C:\Source\nien\vital10-frontend>

i Still get this three errros:

ERROR in ./node_modules/resolve/lib/async.js
Module not found: Error: Can't resolve 'fs' in 'C:\Source\nien\vital10-frontend\node_modules\resolve\lib'
ERROR in ./node_modules/resolve/lib/sync.js
Module not found: Error: Can't resolve 'fs' in 'C:\Source\nien\vital10-frontend\node_modules\resolve\lib'
ERROR in ./node_modules/tslint/lib/utils.js
Module not found: Error: Can't resolve 'fs' in 'C:\Source\nien\vital10-frontend\node_modules\tslint\lib'
i 「wdm」: Failed to compile.
357386 次浏览

你能试试这些步骤吗?

  1. 删除 node_modules
  2. 删除 package-lock.json
  3. package.json中删除 "fs": "0.0.1-security"
  4. npm install --save

这个错误是因为 angular-cli不支持像“ fs”和“ path”这样的节点中的模块

将以下内容添加到“ package.json”文件的根目录中。

"browser": {
"fs": false,
"path": false,
"os": false
}

只需将 {node:'empty'}添加到 webpack.config文件中。

对我来说,解决方案是把这个添加到 webpack 配置中:

config.node = {
fs: 'empty',
}

使用 NuxtJS 的另一个解决方案是:

// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
extend(config, {}) {
config.node = {
fs: 'empty'
}
}
},

如果您使用 NextJS (未经测试,请在评论中确认它是否可用) :

webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
config.node = {
fs: 'empty'
}
return config
},

In other cases, please refer to Anjana Silva post. An edit of your package.json file can do the job!

我在用 Nuxt.JS What worked for me was using @thisismyname solution, and adding it to nuxt.config.js, like this:

// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
extend(config, {}) {
config.node = {
fs: 'empty'
}
}
},

我得到了相同的错误,但这是由于我的错误,因为我正在使用一个网络请求的公理,但在我的应用程序中的一些点,我输入重定向和按回车,我的代码完成导入“导入{重定向}从“下一个/dist/下一个-服务器/服务器/api-utils”; 删除这一行允许我继续“下一个构建”。

请参考以下链接: https://github.com/vercel/next.js/issues/9768

对于 Webpack > 5
更新 webpack.config.js

module.exports = {
...
resolve: {
fallback: {
"fs": false
},
}
}

Can't resolve 'fs' when bundle with webpack # 447

博士

对于为 Node.js 传送数据的人: 将 target: node添加到 webpack.config.js文件。

解释

我知道这是一个角度问题,但是那些为 Node.js 翻译的人必须记住,默认情况下,Webpack 为浏览器目标翻译,在那里你没有只由 Node.js 提供的模块。如果要运行已转换代码的环境确实是 Node.js,那么只需在配置文件中用 target: node标记它。

我也有同样的问题,但是把我的一个文件夹从 c#重命名为 csharp之后,问题就解决了。

对于 Laravel Mix (^ 4.0) ,我用:

mix.webpackConfig({
"node": {
fs: 'empty',
}
});

在 webpack.Mix.js 里

An issue I've encountered was someone added node to the tsconfig.spec.json.

Removing that resolved the issue.

{
"compilerOptions": {
"types": [
...
"node", // Remove this line
]
}
}

For those who are using react or next js. It might happen due to an auto import for json from express!

import { json } from 'express'

所以,只要确保该行不是自动导入的。

它的 webpack 行为的非反应应用程序,你需要添加任何错误返回到外部对象的 webpack 配置。对我来说,‘ response-national-fs’和‘ fs’本身都显示在错误日志中。加上这些,然后重建 externals: { 'react-native-fs': 'reactNativeFs', 'fs': 'reactNativeFs'}

对于 NextJS 12: next.config.js

module.exports = (phase, { defaultConfig }) => {
return {
...defaultConfig,


webpack: (config) => {
config.resolve = {
...config.resolve,
fallback: {
"fs": false,
"path": false,
"os": false,
}
}
return config
},
}
}

对于那些像我一样迷路的人,只要把代码放在下面,在“脚本”之前,

"browser": {
"fs": false,
"path": false,
"os": false
}

在正上方靠近底部的地方添加

devtool: options.devtool ...


node: { fs: 'empty' },

这将导致 webpack 忽略 fs 依赖项。

也就是说。

resolve: {
modules: ['app', 'node_modules'],
extensions: [
'.js',
'.jsx',
'.react.js',
],
mainFields: [
'browser',
'jsnext:main',
'main',
],
},
node: { fs: 'empty' },
devtool: options.devtool,
target: 'web', // Make web variables accessible to webpack, e.g. window
performance: options.performance || {},
});

对于任何人使用幼虫8与幼虫混合 ^ 5

mix.webpackConfig({
resolve: {
fallback: { fs: false }
}
});

这个是 webpack < 5: mix.webpackConfig({ node: { fs: 'empty', } })

把这个加到你的 webpack.mix.js

对于 Electron 的渲染过程,在 webpack 配置文件中更改目标,如下所示:

target: 'electron-renderer'

更多关于电子的 webpack 目标选项,请参阅本页: https://webpack.js.org/configuration/target/

使用 fs 服务器端,这样如果使用 NextJS,就可以在 getStaticProps 等函数中使用它。

我收到了错误,因为我需要从内角电子,并试图使用一个电子函数。 举个例子,不要做这样的事情:

const { dialog } = require('electron');

使用 IPC 向电子过程发送信息:

import { IpcRenderer, IpcRendererEvent } from 'electron';