Home > Blockchain >  extract 7zip files in Nodejs
extract 7zip files in Nodejs

Time:08-08

I am trying to extract .7z files which is password protected. In a particular folder path there are some .7z files format. First I have to extract all files in the same directory than I have to do another stuff with this files.

const path = require('path')
const fs = require('fs')

import { extractFull } from 'node-7z-forall';

const dirpath = path.join('C:/MyFolder/DATA')
   

fs.readdir(dirpath, function(err, files) {
  const txtFiles = files.filter(el => path.extname(el) === '.7z')
  console.log(txtFiles);
  

        extractFull(txtFiles, 'C:/MyFolder/DATA', { p: 'admin123' } /* 7z options/switches */)
       .progress(function (files) {
       console.log('Some files are extracted: %s', files);
});
})

I am using node-7z-forall module but it is only working when I change the file format to .js to .mjs. in .mjs file format file extract smoothly .but in .js format it is not working.

error:

import { extractFull } from 'node-7z-forall';
^^^^^^
SyntaxError: Cannot use import statement outside a module

How to handle this error. Is it possible to work with in .js format instead of .mjs format? I am new in nodejs. Please help!

CodePudding user response:

the reason it errors, it that ".js" indicates a commonjs file which uses require() but a ".mjs" file indicates a module which uses the import syntax. This is also where the error comes from because you try to use import in a non module. You can prevent the error by simply importing the package using require():

const { extractFull } = require('node-7z-forall');
  • Related