Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✘ [ERROR] Could not resolve "aws-sdk" #1704

Closed
GathsaraH opened this issue May 27, 2023 · 3 comments
Closed

✘ [ERROR] Could not resolve "aws-sdk" #1704

GathsaraH opened this issue May 27, 2023 · 3 comments
Labels

Comments

@GathsaraH
Copy link

Issue Summary

I am using electron + sqlite3 + react + vite.
After installing sqlite3 and trying to rerun my application, now I have this error in my console.

This is my console look likes

✘ [ERROR] Could not resolve "mock-aws-s3"

    node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:28:
      43 │     const AWSMock = require('mock-aws-s3');
         ╵                             ~~~~~~~~~~~~~

  You can mark the path "mock-aws-s3" as external to exclude it from the bundle, which will remove
  this error. You can also surround this "require" call with a try/catch block to handle this
  failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "aws-sdk"

    node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:76:22:
      76 │   const AWS = require('aws-sdk');
         ╵                       ~~~~~~~~~

  You can mark the path "aws-sdk" as external to exclude it from the bundle, which will remove this
  error. You can also surround this "require" call with a try/catch block to handle this failure at
  run-time instead of bundle-time.

✘ [ERROR] Could not resolve "nock"

    node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:112:23:
      112 │   const nock = require('nock');
          ╵                        ~~~~~~

  You can mark the path "nock" as external to exclude it from the bundle, which will remove this
  error. You can also surround this "require" call with a try/catch block to handle this failure at
  run-time instead of bundle-time.

/Users/gathsaraumesh/Development/My Work/Project/MyMarkDown/Project Files/app-ink-blend/node_modules/esbuild/lib/main.js:1636
  let error = new Error(`${text}${summary}`);
              ^

Error: Build failed with 3 errors:
node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:28: ERROR: Could not resolve "mock-aws-s3"
node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:76:22: ERROR: Could not resolve "aws-sdk"
node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:112:23: ERROR: Could not resolve "nock"
    at failureErrorWithLog (/Users/gathsaraumesh/Development/My Work/Project/MyMarkDown/Project Files/app-ink-blend/node_modules/esbuild/lib/main.js:1636:15)
    at /Users/gathsaraumesh/Development/My Work/Project/MyMarkDown/Project Files/app-ink-blend/node_modules/esbuild/lib/main.js:1048:25
    at /Users/gathsaraumesh/Development/My Work/Project/MyMarkDown/Project Files/app-ink-blend/node_modules/esbuild/lib/main.js:1512:9
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  errors: [
    {
      detail: undefined,
      id: '',
      location: {
        column: 28,
        file: 'node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js',
        length: 13,
        line: 43,
        lineText: "    const AWSMock = require('mock-aws-s3');",
        namespace: '',
        suggestion: ''
      },
      notes: [
        {
          location: null,
          text: 'You can mark the path "mock-aws-s3" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.'
        }
      ],
      pluginName: '',
      text: 'Could not resolve "mock-aws-s3"'
    },
    {
      detail: undefined,
      id: '',
      location: {
        column: 22,
        file: 'node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js',
        length: 9,
        line: 76,
        lineText: "  const AWS = require('aws-sdk');",
        namespace: '',
        suggestion: ''
      },
      notes: [
        {
          location: null,
          text: 'You can mark the path "aws-sdk" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.'
        }
      ],
      pluginName: '',
      text: 'Could not resolve "aws-sdk"'
    },
    {
      detail: undefined,
      id: '',
      location: {
        column: 23,
        file: 'node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js',
        length: 6,
        line: 112,
        lineText: "  const nock = require('nock');",
        namespace: '',
        suggestion: ''
      },
      notes: [
        {
          location: null,
          text: 'You can mark the path "nock" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.'
        }
      ],
      pluginName: '',
      text: 'Could not resolve "nock"'
    }
  ],
  warnings: []
}

This is my vite config

import { rmSync } from 'node:fs'
import path from 'node:path'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import electron from 'vite-plugin-electron'
import renderer from 'vite-plugin-electron-renderer'
import pkg from './package.json'

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  rmSync('dist-electron', { recursive: true, force: true })

  const isServe = command === 'serve'
  const isBuild = command === 'build'
  const sourcemap = isServe || !!process.env.VSCODE_DEBUG

  return {
    resolve: {
      alias: {
        '@': path.join(__dirname, 'src')
      },
    },
    plugins: [
      react(),
      electron([
        {
          // Main-Process entry file of the Electron App.
          entry: 'electron/main/index.ts',
          onstart(options) {
            if (process.env.VSCODE_DEBUG) {
              console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
            } else {
              options.startup()
            }
          },
          vite: {
            build: {
              sourcemap,
              minify: isBuild,
              outDir: 'dist-electron/main',
              rollupOptions: {
                external: [...Object.keys('dependencies' in pkg ? pkg.dependencies : {}), 'nock', 'mock-aws-s3', 'aws-sdk'],
              },
            },
          },
        },
        {
          entry: 'electron/preload/index.ts',
          onstart(options) {
            // Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
            // instead of restarting the entire Electron App.
            options.reload()
          },
          vite: {
            build: {
              sourcemap: sourcemap ? 'inline' : undefined, // #332
              minify: isBuild,
              outDir: 'dist-electron/preload',
              rollupOptions: {
                external: [...Object.keys('dependencies' in pkg ? pkg.dependencies : {}), 'nock', 'mock-aws-s3', 'aws-sdk'],
              },
            },
          },
        }
      ]),
      // Use Node.js API in the Renderer-process
      renderer(),
    ],
    server: process.env.VSCODE_DEBUG && (() => {
      const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
      return {
        host: url.hostname,
        port: +url.port,
      }
    })(),
    clearScreen: false,
  }
})

``

### Steps to Reproduce

1. Install sqlite3 using npm
2. Try to run npm run dev

### Version

5.1.6

### Node.js Version

16.20.0

### How did you install the library?

Using npm
@GathsaraH GathsaraH added the bug label May 27, 2023
@Vector-Green
Copy link

That won't work that way unfortunately, I have the same issue, you are trying to bundle node-pre-gyp((
as an option there is the only way to use sqlite3-loader but that doen't support cross-compilation(

@fcanela
Copy link

fcanela commented Oct 7, 2023

In case you arrived here looking for a solution, I will share some details that I expect will help you understand why is this happening.

node-sqlite3 depends on node-pre-gyp (an unmaintained library [1]). That library does some conditional requiring of dependencies that are places in "devDependencies", not "dependencies" ([2]) and this is causing troubles with electron+sqlite3 and people trying to use the bcrypt library too.

There are some pointers in the thread about workarounds. I hope they are helpful for you (they weren't for me). I had to implement some workarounds while sqlite3 finds a way out of that unmaintained dependency.

[1] mapbox/node-pre-gyp#657
[2] mapbox/node-pre-gyp#661

@kemalk89
Copy link

I am working on a small side project and I am using Electron Forge with Built-in Template "Vite + TypeScript". (I added React later manually.)

I also faced the issue Could not resolve "mock-aws-s3". By following this guide the problem no longer occurred.

My vite config looks like:

import { defineConfig } from 'vite';

// https://vitejs.dev/config
export default defineConfig({
  build: {
    rollupOptions: {
      // https://www.electronforge.io/config/plugins/vite#native-node-modules
      external: ['sqlite3']
    }
  },
  resolve: {
    // Some libs that can run in both Web and Node.js, such as `axios`, we need to tell Vite to build them in Node.js.
    browserField: false,
    mainFields: ['module', 'jsnext:main', 'jsnext'],
  },
});

daniellockyer added a commit that referenced this issue Dec 24, 2023
…stall`

fixes #1641
fixes #1721
fixes #1714
fixes #1713
fixes #1700
fixes #1704

- `@mapbox/node-pre-gyp` is effectively unmaintained [1] as has a few bugs
  which our users keep running into
- it seems the prebuilt binary world has moved in favor of prebuild +
  it's various other forms
- one option would be to use prebuildify to bundle all binaries into the
  package, but that's a step too far removed from the current situation
  for now
- instead, we can use prebuild-install to download the binaries, and
  `prebuild` to build + upload the binaries
- this means we can remove node-pre-gyp and fix a bunch of issues!

[1]: mapbox/node-pre-gyp#657
daniellockyer added a commit that referenced this issue Dec 24, 2023
…stall`

fixes #1641
fixes #1721
fixes #1714
fixes #1713
fixes #1700
fixes #1704

- `@mapbox/node-pre-gyp` is effectively unmaintained [1] as has a few bugs
  which our users keep running into
- it seems the prebuilt binary world has moved in favor of prebuild +
  it's various other forms
- one option would be to use prebuildify to bundle all binaries into the
  package, but that's a step too far removed from the current situation
  for now
- instead, we can use prebuild-install to download the binaries, and
  `prebuild` to build + upload the binaries
- this means we can remove node-pre-gyp and fix a bunch of issues!
- eventually, we could start providing electron prebuilt binaries too

[1]: mapbox/node-pre-gyp#657
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants