Update to Angular v6 - Module not found: Error: Ca

2020-02-10 03:46发布

I'm trying to migrate my Angular Universal project from Angular v5 to v6

I've got a service where I use fs to load the translation on the server side. Everything works well with Angular v5.

With Angular v6, when I run npm run start aka ng serve --proxy-config proxy.conf.json I face the following error

ERROR in ./src/providers/core/translate/translate-universal-loader.service.ts Module not found: Error: Can't resolve 'fs' in '/Users/me/Documents/projects/myproject/src/providers/core/translate'

In my service I declare fs like the following:

declare var require: any;
const fs = require('fs');

I also tried to declare it like following, but didn't help

import * as fs from 'fs';

To tell webpack to ignore fs I tried to add the following in my webpack.server.config.js without success

node: {
    fs: 'empty'
}

also tried with a webpack plugin, wasn't successful neither

new webpack.IgnorePlugin(/fs/)

but actually it's maybe not the config use by ng serve but I don't know if I could still eject the configuration with v6?

anyone has got an idea?

UPDATE

If I declare fs as any it solves the problem for ng serve but unfortunately it will not work on the server side after npm run build:ssr and run npm run serve. On the server side I will then face the following error

ERROR ReferenceError: fs is not defined

p.s.: my project follows https://github.com/angular/universal-starter structure, config and dependencies

9条回答
聊天终结者
2楼-- · 2020-02-10 03:56

You can declare the fs also by doing this declare var fs: any;

查看更多
家丑人穷心不美
3楼-- · 2020-02-10 03:57

Apparently advanced-json-path resolves this issue in Angular 6 onwards if anyone is using fs

So one has to do an

npm i advanced-json-path --save-dev

as it is a dev dependency (at least in my case) as of this message instance, it is version 1.0.8 Then the Module 'fs' not found doesn't occur.

package.json
{
    ....
   "advanced-json-path": "^1.0.8",
}

In our application it got rid of the Module 'fs' not found error.

查看更多
ゆ 、 Hurt°
4楼-- · 2020-02-10 04:00

I am running an Angular app inside an Angular CLI monorepo, set up with the help of Nx schematics. I’m sharing code from a lib directory.

When trying to use a Typescript Enum from a shared lib inside the Angular app I got:

Critical dependency: the request of a dependency is an expression

Can't resolve 'fs'

The error stack involved:

It was strange because on the face of if the inclusion of the Enum had nothing to do with type-graphql.

The issue turned out to be related to the fact that type-graphql schemas and Typescript Enums were defined in the same lib. Extracting the Enums into their own lib solved the problem. I’m not clear on why the error happened, but in any case the solution worked.

查看更多
forever°为你锁心
5楼-- · 2020-02-10 04:02

Alternatively In NativeScript File is implemented as part of the file system module. To use it you have to import it in your code behind file. e.g.

import * as fs from "file-system';

var documents = fs.knownFolders.documents();
var path = fs.path.join(documents.path, "FileFromPath.txt");
var file = fs.File.fromPath(path);

// Writing text to the file.
file.writeText("Something")
    .then(function () {
        // Succeeded writing to the file.
    }, function (error) {
        // Failed to write to the file.
    });
查看更多
\"骚年 ilove
6楼-- · 2020-02-10 04:02

I fixed this by adding

"types": [
  "node"
]

in tsconfig.app.json

查看更多
Fickle 薄情
7楼-- · 2020-02-10 04:03

Ok after hours I come to the conclusion with the answers I gathered that the real answer is:

You can't use fs anymore in Angular v6

Furthermore, since it's not possible anymore to eject the webpack configuration, there is no way to tell webpack to ignore the fs require

There is an open issue about this subject: https://github.com/angular/angular-cli/issues/10681

P.S.: I was using fs to load the translations on the server side, I overcome the problem by following solution of @xuhcc, see https://github.com/ngx-translate/core/issues/754

Update 2019

See comment, according @Tahlil it is now possible. I didn't tried out.

查看更多
登录 后发表回答