qiskit-community / qiskit-js

:atom_symbol: Qiskit (Quantum Information Science Kit) for JavaScript
114 stars 30 forks source link

require qiskit-js TypeError: fs.readFileSync is not a function #82

Closed adamxy closed 5 years ago

adamxy commented 5 years ago

react@16.8.6 qiskit@0.9.0

TypeError: fs.readFileSync is not a function (anonymous function) D:/mytest/node_modules/@qiskit/qasm/lib/Parser.js:23 20 | const dbg = utils.dbg(__filename); // const QasmError = require('./QasmError'); 21 | // TODO: Do async? 22 |

23 | const bnf = fs.readFileSync(path.resolve(dirname, 'grammar.jison'), 'utf8'); 24 | let parser; 25 | 26 | class Parser { View compiled ./node_modules/@qiskit/qasm/lib/Parser.js http://localhost:3000/static/js/1.chunk.js:1683:30 __webpack_require D:/mytest/webpack/bootstrap:781 778 | }; 779 | 780 | // Execute the module function 781 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId)); | ^ 782 | 783 | // Flag the module as loaded 784 | module.l = true; View compiled

danbev commented 5 years ago

I've not been able to reproduce this issue. It looks from the output that the fs module was not required but in version 0.9.0 is is there. Would you be able to paste the code you are using and I'd be happy to take a closer look.

danbev commented 5 years ago

I did run into a different error when trying to reproduce this and create https://github.com/Qiskit/qiskit-js/pull/83.

adamxy commented 5 years ago

import { qiskit } from 'qiskit';

qiskit.algo.random().then(rand => console.log(JavaScript: ${rand}));

Layne-wu commented 5 years ago

@danbev When the project is started with 'npm', the error will be reported.

adamxy commented 5 years ago

I did run into a different error when trying to reproduce this and create #83.

I have package.json in my project environment dependency.

danbev commented 5 years ago

Below is what I've to reproduce this.

package.json:

{
  "name": "qiskit-parser-issue",
  "version": "0.0.1",
  "license": "Apache-2.0",
  "dependencies": {
    "qiskit": "^0.9.0"
  },
  "type": "module"
}

err.js:

import qiskit from 'qiskit';

qiskit.algo.random().then(rand => console.log(`JavaScript: ${rand}`));

Running with Node.js version v12.4.0:

$ npm install
$ node --experimental-modules err.js
(node:21565) ExperimentalWarning: The ESM module loader is experimental.
JavaScript: 0.8533224881075365
adamxy commented 5 years ago
npm install

Thank you so much!

danbev commented 5 years ago

Thank you so much!

Glad you were able to get it working! Would you be able to close this issue if you consider this it done?

adamxy commented 5 years ago

err.js export function Qiskit(val) { qiskit.algo.random().then(val => console.log(JavaScript: ${val})); return val }

b.js import { Qiskit } from './QiskitJs' const const0 = ` IBMQASM 2.0; include "qelib1.inc"; qreg q[1]; creg c[1];

x q[1]; measure q -> c; `; console.log(Qiskit(val))

When I import this err.js file into another file, I still got an error. The error message is as follows TypeError: fs.readFileSync is not a function

danbev commented 5 years ago

@adamxy Can you push this code somewhere, like a github repo or something, so I can see the exact code and package.json you are using?

adamxy commented 5 years ago

@adamxy Can you push this code somewhere, like a github repo or something, so I can see the exact code and package.json you are using?

I put this code on my github, you can download it. Thanks https://github.com/adamxy/Qiskit-js-test

danbev commented 5 years ago

@adamxy Thanks! Can you also provide the exact steps/commands to run to reproduce the error you are seeing?

adamxy commented 5 years ago

@adamxy Thanks! Can you also provide the exact steps/commands to run to reproduce the error you are seeing?

step1 npm start step2 open chrome browser

danbev commented 5 years ago

This is the output when I run npm start:

Failed to compile.

./src/index.js
Module not found: Can't resolve 'codemirror/mode/quantum/quantum' in '/Users/danielbevenius/work/machine-learning/Qiskit-js-test/mytest/src'

This does not match the error in this issue, is there some step I've missed?

adamxy commented 5 years ago

sorry, please delete the line "import 'codemirror/mode/quantum/quantum'" and update "mode: 'quantum'" to "mode:'python'" in index.js.

danbev commented 5 years ago

@adamxy Thanks for that, I can reproduce it now.

I've taken a quick look but I don't have a quick fix for this and would need to investigate a little. The problem is that this is last day before a four week vacation so I'm afraid I don't have enough time today. I'd be happy to follow up on this when I get back, and will try to take a look later this afternoon before I head off if possible. Sorry about this.

adamxy commented 5 years ago

@adamxy Thanks for that, I can reproduce it now.

I've taken a quick look but I don't have a quick fix for this and would need to investigate a little. The problem is that this is last day before a four week vacation so I'm afraid I don't have enough time today. I'd be happy to follow up on this when I get back, and will try to take a look later this afternoon before I head off if possible. Sorry about this.

Thanks for you ,It doesn't matter.

Layne-wu commented 5 years ago

@adamxy Thanks for that, I can reproduce it now.

I've taken a quick look but I don't have a quick fix for this and would need to investigate a little. The problem is that this is last day before a four week vacation so I'm afraid I don't have enough time today. I'd be happy to follow up on this when I get back, and will try to take a look later this afternoon before I head off if possible. Sorry about this.

We are currently developing a quantum computing platform, so we need to integrate this component, but we're not in a hurry, and you could help us solve it when you come back. Thank you and have a nice vacation!

danbev commented 5 years ago

@wuwanlei @adamxy I've taken a look at this now and have created qiskit-webpack-example which allowed me to get passed the issue reported.

Can you take a look and see if it makes sense and let us know if you have any issues (install/build/run instructions can be found in the readme)?

adamxy commented 5 years ago

Thank you so much!

Glad you were able to get it working!

danbev commented 5 years ago

@jesusprubio Do you think it would make sense to add qiskit-webpack-example to the qiskit-js repository?

It was a little tricky getting this right the first time and it might save others some time if they can find it and use it as a reference without having to search through the issue to find it.

jesusprubio commented 5 years ago

Yes please, just create a example folder or a new npm package if you think it´s better.

Thank you 😄