我正在尝试将我的角通用项目从角v5迁移到v6
我有一个使用fs在服务器端加载翻译的服务。所有的一切都很好的角度v5。
使用角v6时,当我运行npm run start又名ng serve --proxy-config proxy.conf.json时,将面临以下错误
找不到./src/providers/core/translate/translate-universal-loader.service.ts模块中的错误:错误:无法在'/Users/me/Documents/projects/myproject/src/providers/core/translate‘中解析'fs’
在我的服务中,我声明fs如下:
declare var require: any;
const fs = require('fs');我也试图像下面这样声明它,但是没有帮助
import * as fs from 'fs';为了让webpack忽略fs,我尝试在我的webpack.server.config.js中添加以下内容,但没有成功
node: {
fs: 'empty'
}也尝试过用webpack插件,也没有成功。
new webpack.IgnorePlugin(/fs/)但实际上,这可能不是ng serve使用的配置,但我不知道是否还能用v6弹出配置?
有人有主意吗?
更新
如果我将fs声明为any,它解决了ng serve的问题,但不幸的是,在npm run build:ssr和运行npm run serve之后,它将无法在服务器端工作。在服务器端,我将面临以下错误
错误ReferenceError: fs未定义
我的项目遵循https://github.com/angular/universal-starter结构、配置和依赖关系
发布于 2018-05-07 10:24:50
更新2020
见Marc的回答 (角v9 )。
更新2019年
请看评论,据@Tahlil说,现在有可能了。这适用于角v8 (常春藤编译器) 见这个答案。它将特定模块设置为false,以便在package.json中的浏览器中使用。
原始答案
好的,过了几个小时,我得出结论,我得出的结论是,真正的答案是:
您不能再在角fs中使用v6了
此外,由于不能再弹出webpack的配置,所以没有办法让webpack忽略fs的要求
关于这个主题有一个悬而未决的问题:https://github.com/angular/angular-cli/issues/10681
P.S.:我使用fs在服务器端加载翻译,通过遵循@xuhcc的解决方案,我克服了这个问题,请参阅https://github.com/ngx-translate/core/issues/754
发布于 2020-08-15 15:35:32
由于以前的答案已经很老了,在这里强调Angular9的解决方法之一就是在package.json中添加以下内容,这可能会有所帮助:
"browser": {
"fs": false,
"os": false,
"path": false
}这对我很管用。作为参考,我在官方的tensorflow/tfjs页面这里上找到了这个解决方案。
发布于 2018-11-02 12:23:25
对于那些仍在寻找答案的人来说,这是我如何在我的“角7”应用程序中设法要求(Fs)。或者任何其他节点模块。
版本
Angular CLI: 7.0.4
Node: 10.13.0
OS: win32 x64
"@angular/animations": "~7.0.0",
"@angular/common": "~7.0.0",
"@angular/compiler": "~7.0.0",
"@angular/core": "~7.0.0",
"@angular/forms": "~7.0.0",
"@angular/http": "~7.0.0",
"@angular/platform-browser": "~7.0.0",
"@angular/platform-browser-dynamic": "~7.0.0",
"@angular/router": "~7.0.0",
"@angular-devkit/build-angular": "~0.10.0",
"@angular/cli": "~7.0.4",
"@angular/compiler-cli": "~7.0.0",
"@angular/language-service": "~7.0.0",
"electron": "^3.0.7",
"typescript": "~3.1.1"1.安装@type/节点
npm install --save-dev @types/node
2.修改tsconfig.json
注意"allowSyntheticDefaultImports“标志。它必须被设定为真。
{
"compileOnSave": false,
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"types": [
"node"
],
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
],
"strict": false
}
}3.需要fs
import { Component } from '@angular/core';
import { } from 'electron';
import Fs from 'fs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor() {
//check if platform is electron
let isElectron: boolean = window && window['process'] && window['process'].type;
if (isElectron) {
let fs: typeof Fs = window['require']('fs');
let app: Electron.App = window['require']('electron').remote;
console.log(fs, app, window['process']);
}
}
}注意事项:文件顶部的导入语句只是为了提供类型信息。变量值是使用节点require设置的。
有关更新,请在此处跟踪问题。
https://github.com/angular/angular-cli/issues/9827
编辑:
结果是,如果您的项目有require、'fs', 'path', 'child_process'等的依赖项,那么角编译器就无法编译代码。为了解决这个问题,正如有人已经建议的那样,将(window as any).global = window;添加到polyfills.ts中。
https://stackoverflow.com/questions/50202948
复制相似问题