Javascript 使用 import fs from 'fs'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43622337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using import fs from 'fs'
提问by birdybird03
I want to use import fs from 'fs'in JavaScript. Here is a sample:
我想import fs from 'fs'在 JavaScript 中使用。这是一个示例:
import fs from 'fs'
var output = fs.readFileSync('someData.txt')
console.log(output)
The error I get when I run my file using node main.jsis:
使用运行文件时出现的错误node main.js是:
(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?
为了实现从其他地方导入模块和功能,我应该在node中安装什么?
回答by RobertoNovelo
For default exports you should use:
对于默认导出,您应该使用:
import * as fs from 'fs';
Or in case the module has named exports:
或者,如果模块已命名导出:
import {fs} from 'fs';
Example:
例子:
//module1.js
export function function1() {
console.log('f1')
}
export function function2() {
console.log('f2')
}
export default function1;
And then:
进而:
import defaultExport,?{ function1, function2 } from './module1'
defaultExport(); // This calls function1
function1();
function2();
Additionally, you should use webpack or something similar to be able to use es6 import
此外,您应该使用 webpack 或类似的东西才能使用 es6 import
回答by phihag
ES6 modules supportin 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-modulesflag, and it will likely work.
node.js 中的ES6 模块支持是最近才出现的;即使在最前沿的版本中,它仍然是实验性的。使用节点 10,您可以使用--experimental-modules标志启动节点,它可能会工作。
To import on older node versions - or standard node 10 - use CommonJS syntax:
要导入旧节点版本 - 或标准节点 10 - 使用 CommonJS 语法:
const fs = require('fs');
回答by nomis
In order to use import { readFileSync } from 'fs', you have to:
为了使用import { readFileSync } from 'fs',您必须:
- Be using
node 10+ - Use the
--experimental-modulesflag (in node 10), e.g. node --experimental-modules server.mjs (see #3 for explanation of .mjs) - Rename the file extension of your file with the
importstatements, to.mjs, .js will not work, e.g. server.mjs
- 正在使用
node 10+ - 使用
--experimental-modules标志(在节点 10 中),例如 node --experimental-modules server.mjs(有关 .mjs 的解释,请参见 #3) - 使用
import语句重命名文件的文件扩展名, to.mjs, .js will not work,例如 server.mjs
The other answers hit on 1 and 2, but 3 is also necessary. Also, note that this feature is considered extremely experimental at this point (1/10 stability) and not recommended for production, but I will still probably use it.
其他答案出现在 1 和 2 上,但 3 也是必要的。另外,请注意,此时此功能被认为是非常实验性的(1/10 稳定性),不推荐用于生产,但我仍然可能会使用它。
Here's the node 10 ESM documentation
回答by Yoann
Building on RobertoNovelo's answer:
基于 RobertoNovelo 的回答:
import * as fs from 'fs';
is currently the simplest way to do it.
是目前最简单的方法。
Tested with a node project (node v10.15.3), with esm (https://github.com/standard-things/esm#readme) allowing to use import.
使用节点项目(节点 v10.15.3)进行测试,esm(https://github.com/standard-things/esm#readme)允许使用import.
回答by José Silva
The new ECMAScript module support is able natively in Node.js 12
Node.js 12 原生支持新的 ECMAScript 模块
It was released yesterday (2019-04-23) and it means there is no need to use the flag --experimental-modules.
它于昨天(2019-04-23)发布,这意味着不需要使用 flag --experimental-modules。
To read more about it: http://2ality.com/2019/04/nodejs-esm-impl.html
回答by Rajkumar Peter
If we are using TypeScript, we can update the Type definition file by running the command npm install @types/node from the terminal/command prompt.
如果我们使用的是 TypeScript,我们可以通过从终端/命令提示符运行命令 npm install @types/node 来更新类型定义文件。
回答by SakoBu
It's not supported just yet.... If you want to use you will have to install babel
目前还不支持.... 如果你想使用你必须安装 babel

