有没有办法import
使用 Babelify 时的原生 Node 模块(例如 crypto
、fs
、path
)与 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与 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.
来源:
关于javascript - Babelify 和原生 Node 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34604590/