Search code examples
javascriptyarnpkg

Javascript: Problem of javascript file import in yarn build


I have two JavaScript codes.

I would like to import and use another one from one javaScript code.

I tried a few things to import, but there was an error.

This is my main code(main.js):

    import * as test from "./noise";

    const audioContext = new AudioContext({sampleRate: 48000});
    const destination = audioContext.createMediaStreamDestination();

    await NoiseNode.register(audioContext)

And, This is the code that I want to import(noise.js):

    window.NoiseNode = (window.AudioWorkletNode || (window.AudioWorkletNode = window.webkitAudioWorkletNode)) &&
    class extends AudioWorkletNode {
        static async register(a) {
            k = await h;
            await a.audioWorklet.addModule(g + "other.js");
        }}

If i try to import as below,

import * as test from "./noise;

These errors occur:

Line 141:15:  'NoiseNode' is not defined  no-undef

and then, If i try to import as below,

import * as NoiseNode from "./noise";

These errors occur:

Attempted import error: 'register' is not exported from './noise' (imported as 'NoiseNode').

How can I get everything at once?

Please give me some advice.

Thank you.


Solution

  • You have to export NoiseNode before importing it.

    export window.NoiseNode = (window.AudioWorkletNode || (window.AudioWorkletNode = window.webkitAudioWorkletNode)) &&
    

    after exporting, you may import it using the following code:

    import * as NoiseNode from "./noise";