Babelify 和原生 Node 模块

Babelify and native Node modules

有办法import native Node modules (e.g. crypto, fs, path) when using Babelify with Browserify吗?

例如:

'use strict';

import $ from 'jquery';
import fs from 'fs'; // <------ this line causes an error

var data = JSON.parse(fs.readFileSync('foo.json', 'utf8'));

$(document).ready(function () {
  // stuff
});

当我尝试 运行 时,Browserify 给我这个错误:

Error: tried to statically call { readFile: [Function: readFile], readFileSync: [Function: readFileSync], readdir: [Function: readdir], readdirSync: [Function: readdirSync] } as a function while parsing file: /home/vincent/www/project1/resources/js/foo.js while parsing file: /home/vincent/www/project1/resources/js/foo.js

我也尝试了以下相同的结果:

import * as fs from 'fs';
import { fs } from fs;

这是 a known issue with brfs 和 Babelify:

It will eventually be possible once static-module can handle ES6 imports. For now you need to 'require' brfs with CommonJS syntax, and run the brfs transform after babelify.

来源: