gpt4 book ai didi

javascript - Babelify 和原生 Node 模块

转载 作者:行者123 更新时间:2023-11-30 12:08:07 33 4
gpt4 key购买 nike

有没有办法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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com