Error accessing libarchive.js
wachidmudi opened this issue · 7 comments
I'm trying to use this libarchive.js package with node, but i got the following error:
Message:
I tried to make use of require instead of import as well but still no luck!
I'm using node v10.16.0
This is my code:
import {Archive} from 'libarchive.js/main.js';
Archive.init({
workerUrl: 'libarchivejs/dist/worker-bundle.js'
});
const zipFile = 'zip/files.zip';
const files = async (e) => {
const archive = await Archive.open(zipFile);
let obj = await archive.getFilesArray();
console.log(obj);
};
Am i doing it wrong or something?
This library uses esm modules so you need to use esm modules as well, either by transpiling using babel or by using https://github.com/standard-things/esm
Ah i see, my bad. Thanks.
Sorry for reopening this again, but i'm still unable to implement libarchive myself. When i use esm, i'm putting my code into main.js as the docs says, but then i got error.
It says "Worker is not define", hmmm after a few search, it seems that i should use libarchive in a browser such as chrome.
Fine, then i made a simple express in the main.js like this:
import express from 'express';
const path = require('path');
const app = express();
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'index.html')));
const port = 5000;
app.listen(port, () => console.log(`Server started on port ${port}`))
And inside index.html file (i took it from test directory)
I changed this line :
import {Archive} from '../../src/libarchive.js';
Archive.init({
workerUrl: '../../dist/worker-bundle.js'
});
into this :
import {Archive} from './node_modules/libarchive.js/src/libarchive.js';
Archive.init({
workerUrl: './node_modules/libarchive.js/dist/worker-bundle.js'
});
But it's just keep logging http://localhost:5000/node_modules/libarchive.js/src/libarchive.js net::ERR_ABORTED 404 (Not Found)
, i dunno if this the right thing to do or not. I just tried it.
And finally, i'm just wondering if there are some premade quick-start-example for using this libarchive package, either in node or browser.
Very want to use this package because support of many archive format and on top of that, it build up with wasm wich i think it's soo cool.
I would suggest to use webpack for your front-end project
npm install webpack copy-webpack-plugin --save-dev
besides general webpack configuration you'll only need to copy libarchive.js/dist folder as is using copy-webpack-plugin
const CopyPlugin = require('copy-webpack-plugin');
module.exports = {
entry: './your-app.js',
output: {
filename: 'your-dist/your-bundle.js'
},
plugins: [
new CopyPlugin([
{ from: 'node_modules/libarchive.js/dist', to: 'your-dist/dist' },
]),
],
};
and when initializing libarchive.js you need to make sure to point to your-dist/dist directory from browser similar to
import {Archive} from 'libarchive.js';
Archive.init({
workerUrl: 'your-dist/dist/worker-bundle.js'
});
It seems that esm is doing fine, because i can use import statement inside index.js, the problem is that when i call import {Archive} from './node_modules/libarchive.js/main.js';
, it's just won't find the right file http://localhost:5000/node_modules/libarchive.js/main.js net::ERR_ABORTED 404 (Not Found)
.
Then, i realize that what if there are a posibilty that it won't find because it's in a client side.
And then after some research, it turn out that express is just not recognizing what i'm referring to.
After that, i search about how can i referencing node_modules inside express, and got the answer from here
As mention from that, then in my main.js i added this line of code:
app.use('/lib', express.static(__dirname + '/node_modules/libarchive.js'));
app.use('/lib', express.static(__dirname + '/node_modules/libarchive.js/dist'));
and in index.html
import {Archive} from '/lib/main.js';
Archive.init({
workerUrl: '/lib/worker-bundle.js'
});
Finally, it work like a charm. For now, i can use libarchive.js (in browser).
Sorry for my lack of knowledge.
I think your solution is still better, I'll try it another time.
Thanks in advance.
Change it to
Archive.init({
workerUrl: new Worker(new URL('./worker-bundle.js', import.meta.url))
});
Store the file './worker-bundle.js' locally and give that path.
Change it to
Archive.init({ workerUrl: new Worker(new URL('./worker-bundle.js', import.meta.url)) });
Store the file './worker-bundle.js' locally and give that path.
Hello, I'm sorry to hear that you're experiencing an error when trying to call the function according to the instructions in the readme file. Can you please provide me with the specific error message? This will help me better understand the issue and assist you in finding a solution.