javascript - Babelify 和原生 Node 模块

标签 javascript node.js ecmascript-6 babeljs

有没有办法import使用 Babelify 时的原生 Node 模块(例如 cryptofspath)与 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 issuebrfs和 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/

相关文章:

javascript - 在多个条件下过滤 javascript 数组

typescript - 在 Typescript 中使用 ECMA6 数组方法?

javascript - Express 路由器回调中 Array.includes() 始终为 false,Array.indexOf() 始终为 -1 且 Array.find() 始终未定义

javascript - 即使页面重新加载也保持 chrome javascript 运行

javascript - createMediaElementSource()性能

javascript - javascript面向对象编程中super()构造函数和子类方法的执行顺序是什么?

node.js - 使用 GMail SMTP OAuth2 身份验证的 Nodemailer 正在获取 'Invalid status code 401'

javascript - 在 WordPress 中插入 html 和 javascript

html - 如何在Nodejs中读取和解析html?

node.js - Node JS 流 : Understanding data concatenation