Using import fs from 'fs'

JavascriptEs6 Modules

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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');

Solution 3 - Javascript

In order to use import { readFileSync } from 'fs', you have to:

  1. Be using Node.js 10 or later
  2. Use the --experimental-modules flag (in Node.js 10), e.g. node --experimental-modules server.mjs (see #3 for explanation of .mjs)
  3. 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.

Solution 4 - Javascript

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.

Solution 5 - Javascript

Go to package.json file and add:

"type": "module"

This worked for me!

Solution 6 - Javascript

The new ECMAScript module support is able natively in Node.js 12 

It was released on 2019-04-23 and it means there is no need to use the flag --experimental-modules.

To read more about it:

The new ECMAScript module support in Node.js 12

Solution 7 - Javascript

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.

Solution 8 - Javascript

It's not supported just yet... If you want to use it you will have to install Babel.

Solution 9 - Javascript

hey friends node js use commonjs not modaljs so use alawys use const fs = require('fs') for file system if we use modaljs import fs from 'fs' its give error se in termile import {fs} from 'fs'; ^^^^^^ SyntaxError: Cannot use import statement outside a module

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbirdybird03View Question on Stackoverflow
Solution 1 - JavascriptRobertoNoveloView Answer on Stackoverflow
Solution 2 - JavascriptphihagView Answer on Stackoverflow
Solution 3 - JavascriptnomisView Answer on Stackoverflow
Solution 4 - JavascriptYoannView Answer on Stackoverflow
Solution 5 - JavascriptAnurag MaheshwariView Answer on Stackoverflow
Solution 6 - JavascriptJosé SilvaView Answer on Stackoverflow
Solution 7 - JavascriptRajkumar PeterView Answer on Stackoverflow
Solution 8 - JavascriptSakoBuView Answer on Stackoverflow
Solution 9 - JavascriptUsama ijazView Answer on Stackoverflow