admin管理员组文章数量:1129454
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)
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.js in order to achieve importing modules and functions from other places?
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)
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.js in order to achieve importing modules and functions from other places?
Share Improve this question edited Oct 3, 2020 at 19:23 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Apr 25, 2017 at 23:01 escplat12escplat12 2,4914 gold badges25 silver badges35 bronze badges 3 |11 Answers
Reset to default 219For 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
ES6 modules support in Node.js is fairly recent; even in the bleeding-edge versions, it is still experimental. With Node.js 10, you can start Node.js with the --experimental-modules
flag, and it will likely work.
To import on older Node.js versions - or standard Node.js 10 - use CommonJS syntax:
const fs = require('fs');
Building on RobertoNovelo's answer:
import * as fs from 'fs';
is currently the simplest way to do it.
It was tested with a Node.js project (Node.js v10.15.3), with esm, allowing to use import
.
In order to use import { readFileSync } from 'fs'
, you have to:
- Be using Node.js 10 or later
- Use the
--experimental-modules
flag (in Node.js 10), e.g.node --experimental-modules server.mjs
(see #3 for explanation of .mjs) - Rename the file extension of your file with the
import
statements, to.mjs
, .js will not work, e.g. 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.
Here's the Node.js 10 ESM documentation.
Go to package.json file and add:
"type": "module"
This worked for me!
It is 2023, In the current version node 16+:
import {readFileSync} from "fs"
const string_output = readFileSync("path-to-file", 'utf8')
// the rest
If you do not give the second argument, encoding 'utf8', it will return a binary file
If we are using TypeScript, we can update the type definition file by running the command npm install @types/node
from the terminal or command prompt.
If you want your statement import fs from 'fs' to be executable, you can make your file extension .mjs instead of .js. i.e filename.mjs
The new ECMAScript module support is able natively in Node.js 12
本文标签: javascriptUsing import fs from 39fs39Stack Overflow
版权声明:本文标题:javascript - Using import fs from 'fs' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736745896a1950771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
import fs from 'fs/promises'
syntax, on earlier versions, you'll have to create an alias usingimport { promises as fs } from syntax
– imrok Commented Jan 27, 2021 at 9:07