代码之家  ›  专栏  ›  技术社区  ›  An-droid

使用具有角度6的节点路径模块

  •  1
  • An-droid  · 技术社区  · 6 年前

    我找到这个帖子来解决这个问题:

    https://gist.github.com/niespodd/1fa82da6f8c901d1c33d2fcbb762947d

    上面写着要添加脚本:

    const fs = require('fs');
    const f = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js';
    
    fs.readFile(f, 'utf8', function (err,data) {
      if (err) {
        return console.log(err);
      }
      var result = data.replace(/node: false/g, 'node: {crypto: true, stream: true}');
    
      fs.writeFile(f, result, 'utf8', function (err) {
        if (err) return console.log(err);
      });
    });
    

    并在包.json:

    {...
      "scripts": {
        "postinstall": "node patch.js",
        ...
      }
    }
    

    import {join} from 'path';
    

    它表示找不到模块路径。


    2 回复  |  直到 6 年前
        1
  •  7
  •   Haijin    6 年前

    有趣的问题。

    我设法得到路径模块的工作,我的角度项目。

      npm install path 
    

    这是发布到NPM注册表的NodeJS路径模块的精确副本。

    虽然后来我删除了这个模块和路径模块似乎仍然工作。

    3、使用

    node patch.js
    

    我手动运行它并转到“node\u modules/@angular devkit/build angular/src/angular cli files/models/webpack configs”/浏览器.js'以检查实际更改的文件。

    4,我把

      import {join} from 'path';
    

    在我的一个房间里组件.ts文件

     let x = join('Users', 'Refsnes', '..', 'demo_path.js');
        console.log("-------------------------------------------------");
        console.log(x);
    

    然后跑 "ng serve"

    -------------------------------------------------
    Users/demo_path.js
    

    所以这个方法确实有效。我不知道你哪一步做错了。我的猜测将是第一步,因为我尝试如果不做步骤3有不同的错误信息。请检查您的node\u modules文件夹并验证path文件夹是否存在,如有必要请重新安装。

        2
  •  -1
  •   Tim spar    6 年前
    1. npm install --save-dev @types/node
    2. import * as path from 'path';
    推荐文章