使用'fs'中的导入fs [英] Using import fs from 'fs'

查看:852
本文介绍了使用'fs'中的导入fs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaScript中使用从'fs'导入fs。下面是一个示例:

I want to use import fs from 'fs' in JavaScript. Here is a sample:

import fs from 'fs'

var output = fs.readFileSync('someData.txt')

console.log(output)

我使用节点main.js 运行我的文件时得到的错误是:

The error I get when I run my file using node main.js is:

(function (exports, require, module, __filename, __dirname) { import fs from 'fs
'
                                                              ^^^^^^
SyntaxError: Unexpected token import

为了从其他地方导入模块和函数,我应该在节点中安装什么?

What should I install in node in order to achieve importing modules and functions from other places?

推荐答案

ES6模块支持 node.js是相当新的;即使在最前沿的版本中,它仍然是实验性的。使用节点10,您可以使用 - experimental-modules 标志启动节点,它可能会有效。

ES6 modules support in node.js is fairly recent; even in the bleeding-edge versions, it is still experimental. With node 10, you can start node with the --experimental-modules flag, and it will likely work.

要导入旧节点版本 - 或标准节点10 - 使用CommonJS语法:

To import on older node versions - or standard node 10 - use CommonJS syntax:

const fs = require('fs');

这篇关于使用'fs'中的导入fs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆