serenadeai / speech-recorder

speech-recorder is a node.js module for streaming audio from a device's microphone and filtering for speech.
MIT License
86 stars 19 forks source link

How to use this library with Webpack? #20

Closed bholtbholt closed 2 years ago

bholtbholt commented 2 years ago

👋 I'm having trouble using this library with Webpack.

I'm working on a VSCode Extension (which uses Electron under the hood and builds like a node library). I'm using Webpack to bundle as the docs say. I'm on a 2017 Mac OSX 12.1.

When I compile I get 1 error and 1 warning:

I believe both of these errors are a gap with connecting the binaries (.node) to the Webpack build.

So far I've tried:

All of these steps have the same runtime error: Could not find module root given file: "internal/modules/cjs/loader.js". Do you have apackage.jsonfile? . I have a package.json in the repo.

Do you have any suggestions? Am I missing something in the setup?

Errors below

WARNING in ./node_modules/speech-recorder/dist/index.js 71:24-46
Critical dependency: the request of a dependency is an expression
    at CommonJsRequireContextDependency.getWarnings (/Users/brianholt/Sites/vippets/node_modules/webpack/lib/dependencies/ContextDependency.js:91:18)
    at Compilation.reportDependencyErrorsAndWarnings (/Users/brianholt/Sites/vippets/node_modules/webpack/lib/Compilation.js:3127:24)
    at /Users/brianholt/Sites/vippets/node_modules/webpack/lib/Compilation.js:2724:28
    at _next2 (eval at create (/Users/brianholt/Sites/vippets/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:16:1)
    at eval (eval at create (/Users/brianholt/Sites/vippets/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:42:1)
    at /Users/brianholt/Sites/vippets/node_modules/webpack/lib/FlagDependencyExportsPlugin.js:385:11
    at /Users/brianholt/Sites/vippets/node_modules/neo-async/async.js:2830:7
    at Object.each (/Users/brianholt/Sites/vippets/node_modules/neo-async/async.js:2850:39)
    at /Users/brianholt/Sites/vippets/node_modules/webpack/lib/FlagDependencyExportsPlugin.js:361:18
    at /Users/brianholt/Sites/vippets/node_modules/neo-async/async.js:2830:7
ModuleDependencyWarning: Critical dependency: the request of a dependency is an expression
    at Compilation.reportDependencyErrorsAndWarnings (/Users/brianholt/Sites/vippets/node_modules/webpack/lib/Compilation.js:3132:23)
    at /Users/brianholt/Sites/vippets/node_modules/webpack/lib/Compilation.js:2724:28
    at _next2 (eval at create (/Users/brianholt/Sites/vippets/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:16:1)
    at eval (eval at create (/Users/brianholt/Sites/vippets/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:42:1)
    at /Users/brianholt/Sites/vippets/node_modules/webpack/lib/FlagDependencyExportsPlugin.js:385:11
    at /Users/brianholt/Sites/vippets/node_modules/neo-async/async.js:2830:7
    at Object.each (/Users/brianholt/Sites/vippets/node_modules/neo-async/async.js:2850:39)
    at /Users/brianholt/Sites/vippets/node_modules/webpack/lib/FlagDependencyExportsPlugin.js:361:18
    at /Users/brianholt/Sites/vippets/node_modules/neo-async/async.js:2830:7
    at Object.each (/Users/brianholt/Sites/vippets/node_modules/neo-async/async.js:2850:39)
 @ ./src/extension.ts 5:26-52

ERROR in ./node_modules/onnxruntime/bin/napi-v3/darwin/x64/onnxruntime_binding.node 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
ModuleParseError: Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
    at handleParseError (/Users/brianholt/Sites/vippets/node_modules/webpack/lib/NormalModule.js:969:19)
    at /Users/brianholt/Sites/vippets/node_modules/webpack/lib/NormalModule.js:1088:5
    at processResult (/Users/brianholt/Sites/vippets/node_modules/webpack/lib/NormalModule.js:793:11)
    at /Users/brianholt/Sites/vippets/node_modules/webpack/lib/NormalModule.js:853:5
    at /Users/brianholt/Sites/vippets/node_modules/loader-runner/lib/LoaderRunner.js:406:3
    at iterateNormalLoaders (/Users/brianholt/Sites/vippets/node_modules/loader-runner/lib/LoaderRunner.js:232:10)
    at /Users/brianholt/Sites/vippets/node_modules/loader-runner/lib/LoaderRunner.js:223:4
    at /Users/brianholt/Sites/vippets/node_modules/webpack/lib/NormalModule.js:827:15
    at Array.eval (eval at create (/Users/brianholt/Sites/vippets/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:12:1)
    at runCallbacks (/Users/brianholt/Sites/vippets/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:27:15)
 @ ./node_modules/onnxruntime/bin/napi-v3/ sync ^\.\/.*\/.*\/onnxruntime_binding\.node$ ./darwin/x64/onnxruntime_binding.node
 @ ./node_modules/onnxruntime/lib/binding.js 9:0-86
 @ ./node_modules/onnxruntime/lib/inference-session-impl.js 20:18-38
 @ ./node_modules/onnxruntime/lib/inference-session.js 6:33-68
 @ ./node_modules/onnxruntime/lib/index.js 15:13-43
 @ ./node_modules/speech-recorder/dist/vad.js 55:39-61
 @ ./node_modules/speech-recorder/dist/index.js 67:28-44
 @ ./src/extension.ts 5:26-52

2022-01-01 22:21:41: webpack 5.65.0 compiled with 1 error and 1 warning in 2105 ms (998d22aca141234e6a49)
jonoirwinrsa commented 2 years ago

@bholtbholt did you find a solution to this issue? I have the same problem.

bholtbholt commented 2 years ago

@jonoirwinrsa no, I didn't get anywhere with it 😔

tmacwill commented 2 years ago

hmm, mind pasting your webpack config here? we use this package in our webpack electron application without any special config (i.e., npm install and nothing else).

bholtbholt commented 2 years ago

Yep, here's webpack.config.js and package.json

Webpack config

//@ts-check

"use strict";

const path = require("path");

//@ts-check
/** @typedef {import('webpack').Configuration} WebpackConfig **/

/** @type WebpackConfig */
const extensionConfig = {
  target: "node", // vscode extensions run in a Node.js-context 📖 -> https://webpack.js.org/configuration/node/
  mode: "none", // this leaves the source code as close as possible to the original (when packaging we set this to 'production')
  // node: {
  //   // eslint-disable-next-line @typescript-eslint/naming-convention
  //   __dirname: false,
  // },
  entry: "./src/extension.ts", // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
  output: {
    // the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/
    path: path.resolve(__dirname, "dist"),
    filename: "extension.js",
    libraryTarget: "commonjs2",
  },
  externals: {
    // "speech-recorder": "system speech-recorder",
    vscode: "commonjs vscode", // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
    // modules added here also need to be added in the .vscodeignore file
  },
  resolve: {
    // support reading TypeScript and JavaScript files, 📖 -> https://github.com/TypeStrong/ts-loader
    extensions: [".ts", ".js"],
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        exclude: /node_modules/,
        use: [
          {
            loader: "ts-loader",
          },
        ],
      },
      // {
      //   test: /\.node$/,
      //   loader: "node-loader",
      // },
    ],
  },
  devtool: "nosources-source-map",
  infrastructureLogging: {
    level: "log", // enables logging required for problem matchers
  },
};
module.exports = [extensionConfig];

Package.json

{
  "engines": {
    "vscode": "^1.63.0"
  },
  "activationEvents": [
    "onCommand:vippets.startRecording"
  ],
  "main": "./dist/extension.js",
  "contributes": {
    "commands": []
  },
  "scripts": {
    "vscode:prepublish": "npm run package",
    "compile": "webpack --stats verbose",
    "watch": "webpack --watch",
    "package": "webpack --mode production --devtool hidden-source-map",
    "compile-tests": "tsc -p . --outDir out",
    "watch-tests": "tsc -p . -w --outDir out",
    "pretest": "npm run compile-tests && npm run compile && npm run lint",
    "lint": "eslint src --ext ts",
    "test": "node ./out/test/runTest.js",
    "postinstall": "npm run rebuild",
    "rebuild": "electron-rebuild -v 11.1.1 -w speech-recorder -f"
  },
  "devDependencies": {
    "@types/glob": "^7.1.4",
    "@types/mocha": "^9.0.0",
    "@types/node": "14.x",
    "@types/vscode": "^1.63.0",
    "@typescript-eslint/eslint-plugin": "^5.1.0",
    "@typescript-eslint/parser": "^5.1.0",
    "@vscode/test-electron": "^1.6.2",
    "electron-rebuild": "^3.2.5",
    "eslint": "^8.1.0",
    "glob": "^7.1.7",
    "mocha": "^9.1.3",
    "prettier": "^2.5.1",
    "ts-loader": "^9.2.5",
    "typescript": "^4.4.4",
    "webpack": "^5.52.1",
    "webpack-cli": "^4.8.0"
  },
  "dependencies": {
    "speech-recorder": "^1.4.0"
  }
}
tmacwill commented 2 years ago

thanks! to make sure I’m interpreting this correctly, this is adding speech-recorder as a dependency for a vscode plugin? (haven’t tried that specifically before, so not sure if vscode imposes any constraints)

bholtbholt commented 2 years ago

Yep, that's correct. From what I learned, VSCode uses Electron under the hood, so I thought I would be able to use any Electron package with the right configuration.

VSCode imposes restrictions around audio/video inputs (at least in January 2022), but I was spiking to see if I could use this package to get around them. I couldn't successfully get past the errors above though.

tmacwill commented 2 years ago

got it, thanks! we can look into what it would take to support a vscode extension specifically. in the meantime, this library should work when building an electron app with webpack, but anyone can feel free to reopen + post their application configuration if not!