Is there a way to import native Node modules (e.g. crypto, fs, path) when using Babelify with Browserify?
For example:
'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 gives me this error when I try to run it:
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
I've also tried the following with the same results:
import * as fs from 'fs'; import { fs } from fs;
fsexports something, it is a module after all. Looking at its source, it does something similar toexports.foo = 42;, soimport * as fs from 'fs';should work IMO. This may help: 2ality.com/2015/12/babel-commonjs.html