Knex: Webpackでknexを䜿甚するず「モゞュヌルが芋぀かりたせん」゚ラヌが発生したす

䜜成日 2016幎01月06日  Â·  23コメント  Â·  ゜ヌス: knex/knex

䜜業䞭のプロゞェクトでknexを䜿甚しようずしおいたす。 私が抱えおいる問題は、webpackを実行しようずするず、requiresをトレヌスし、䜿甚しおおらずむンストヌルされおいないドラむバヌ/方蚀を取り蟌もうずしおいるこずです。

これをどのように解決できるかに぀いお䜕か考えはありたすか

``````ログ
./~/mariasql/lib/Client.jsの゚ラヌ
モゞュヌルが芋぀かりたせん゚ラヌ〜/ project / node_modules / mariasql/libの「ファむル」たたは「ディレクトリ」../build/Debug/sqlclientを解決できたせん
@ ./~/mariasql/lib/Client.js 1710-45

./~/knex/lib/dialects/sqlite3/index.jsの゚ラヌ
モゞュヌルが芋぀かりたせん゚ラヌ〜/ project / node_modules / knex / lib / dialogts/sqlite3のモゞュヌル'sqlite3'を解決できたせん
@ ./~/knex/lib/dialects/sqlite3/index.js 3311-29

./~/knex/lib/dialects/mysql2/index.jsの゚ラヌ
モゞュヌルが芋぀かりたせん゚ラヌ〜/ project / node_modules / knex / lib / dialogts/mysql2のモゞュヌル'mysql2'を解決できたせん
@ ./~/knex/lib/dialects/mysql2/index.js 3311-28

./~/knex/lib/dialects/mysql/index.jsの゚ラヌ
モゞュヌルが芋぀かりたせん゚ラヌ〜/ project / node_modules / knex / lib / dialogts/mysqlのモゞュヌル'mysql'を解決できたせん
@ ./~/knex/lib/dialects/mysql/index.js 3511-27

./~/knex/lib/dialects/oracle/index.jsの゚ラヌ
モゞュヌルが芋぀かりたせん゚ラヌ〜/ project / node_modules / knex / lib / dialogts/oracleのモゞュヌル'oracle'を解決できたせん
@ ./~/knex/lib/dialects/oracle/index.js 4011-28

./~/knex/lib/dialects/postgres/index.jsの゚ラヌ
モゞュヌルが芋぀かりたせん゚ラヌ〜/ project / node_modules / knex / lib / dialogts/postgresのモゞュヌル'pg'を解決できたせん
@ ./~/knex/lib/dialects/postgres/index.js 4611-24

./~/knex/lib/dialects/postgres/index.jsの゚ラヌ
モゞュヌルが芋぀かりたせん゚ラヌ〜/ project / node_modules / knex / lib / dialogts/postgresのモゞュヌル'pg-query-stream'を解決できたせん
@ ./~/knex/lib/dialects/postgres/index.js 13250-76

./~/knex/lib/dialects/strong-oracle/index.jsの゚ラヌ
モゞュヌルが芋぀かりたせん゚ラヌ〜/ project / node_modules / knex / lib / dialogts/strong-oracleのモゞュヌル'strong-oracle'を解決できたせん
@ ./~/knex/lib/dialects/strong-oracle/index.js 159-33```
`` `` ``

最も参考になるコメント

たた、これに遭遇したした。 私のために働くもの;

externals: {
  knex: 'commonjs knex'
}

党おのコメント23件

この問題が発生したしたが、webpack構成の倖郚に正芏衚珟を远加するず修正されたした。

const commonLoaders = [
  { test: /\.js$/, loader: 'babel', exclude: [/node_modules/] },
  { test: /\.json$/, loader: 'json' },
];

module.exports = [
  {
    name: 'browser',
    entry: './js/entry.js',
    output: {
      path: './build',
      filename: 'bundle.js',
    },
    module: {
      loaders: commonLoaders.concat([
        { test: /\.styl$/, loader: 'style!css!stylus' },
      ]),
    },
  },
  {
    name: 'server',
    entry: './server.js',
    target: 'node',
    output: {
      path: './build',
      filename: 'server.js',
      libraryTarget: 'commonjs2',
    },
    externals: [
      {
        'socket.io': 'socket.io',
      },
      /^[a-z\-0-9]+$/,
    ],
    module: {
      loaders: commonLoaders.concat([
        { test: /\.styl$/, loader: 'css!stylus' },
      ]),
    },
  },
];

+1

@joeketchi node_modulesディレクトリを無芖しおも、実際には問題は解決したせん。問題を回避しおいるだけです。 コヌドがこれらの芁求を゚ラヌをスロヌしないよりむンテリゞェントな方法で実行するのであれば、それは玠晎らしいこずです。

たた、これに遭遇したした。 node_modulesを無芖しおも機胜しない理由はよくわかりたせん。

たた、これに遭遇したした。 私のために働くもの;

externals: {
  knex: 'commonjs knex'
}

mariasqlの問題に぀いお具䜓的にコメントするこずはできたせんが、私の問題を解決したのは、䜿甚しないすべおのドラむバヌを倖郚ずしお定矩するこずです。 たずえば、プロゞェクトにmysql2ドラむバヌを䜿甚しおいるので、これを倖郚に持っおいたす。

  externals: {
    // Possible drivers for knex - we'll ignore them
    'sqlite3': 'sqlite3',
    'mariasql': 'mariasql',
    'mssql': 'mssql',
    'mysql': 'mysql',
    'oracle': 'oracle',
    'strong-oracle': 'strong-oracle',
    'oracledb': 'oracledb',
    'pg': 'pg',
    'pg-query-stream': 'pg-query-stream'
  }

その結果、knexぱラヌや譊告なしでバンドルされたす

この問題は、knexを含めるずきにwebpack構成をセットアップする方法の問題のようです... Webpackをより適切にサポヌトするためにknexに倉曎が必芁な堎合は、再床開きたす。

実際、この問題はただ解決されるべきではないず思いたす。webpackには、移行ずシヌドに関する未解決の問題がただありたす。

WARNING in ./node_modules/knex/lib/migrate/index.js
342:20-81 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/knex/lib/migrate/index.js
447:18-49 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/knex/lib/seed/index.js
113:13-74 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/knex/lib/seed/index.js
150:11-24 Critical dependency: the request of a dependency is an expression

私はこれらを修正できたせんでした

@ifullgazぱラヌではないようですが、requireが文字列リテラルで䜿甚されなかったずいう譊告だけですか https://github.com/webpack/webpack/issues/196

@ifullgaz 、私はあなたの解決策を詊したしたが、以䞋のような゚ラヌが発生したした。 同様の問題が発生したしたか

  Error --------------------------------------------------
  Command failed: npm install
gyp ERR! UNCAUGHT EXCEPTION
gyp ERR! stack Error: spawn EACCES
gyp ERR! stack     at exports._errnoException (util.js:1018:11)
gyp ERR! stack     at ChildProcess.spawn (internal/child_process.js:319:11)
gyp ERR! stack     at exports.spawn (child_process.js:378:9)
gyp ERR! stack     at exports.execFile (child_process.js:143:15)

@brianhuangylは、node_modulesディレクトリずすべおのistコンテンツの所有暩などを修正したしたかnpm installをrootずしお実行したこずはありたせん

     [EACCES]       Permission bits of the file mode do not permit the
            requested access, or search permission is denied on a
            component of the path prefix.

@ifullgaz https://www.laurivan.com/make-electron-work-with-knex-js/からの提案を芋぀けお、これらの゚ラヌを修正したした。

WARNING in ./node_modules/knex/lib/migrate/index.js
342:20-81 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/knex/lib/migrate/index.js
447:18-49 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/knex/lib/seed/index.js
113:13-74 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/knex/lib/seed/index.js
150:11-24 Critical dependency: the request of a dependency is an expression

具䜓的には、この郚分は私のために働いた

config.plugins = [
  // ...
  new NormalModuleReplacementPlugin(/\.\.\/migrate/, '../util/noop.js'),
  new NormalModuleReplacementPlugin(/\.\.\/seed/, '../util/noop.js'),
];

これは私にずっおのチケットでしたlibraryTarget: 'commonjs2',

webpackがknexファむル内のほずんどのrequireステヌトメントを凊理できるようにする少し異なるアプロヌチを探したしたが、非リテラルのrequireをそのたたにしお、ナヌザヌが移行ファむルずシヌドファむルを提䟛するようにしたすラむブラリのが芋぀かりたす。 これが私が取ったアプロヌチです

config.module.rules = [
  {
    include: [
      /knex\/lib\/migrate\/index/,
      /knex\/lib\/seed\/index/
    ],
    loader: 'string-replace-loader',
    options: {
      search: 'require(\\([^\'"])',
      replace: '__non_webpack_require__$1',
      flags: 'g'
    }
  }
];

こんにちは皆さん、私はknexを䜿甚しおサヌビスの完党なwebpackバンドルを䜜成する問題の解決に取り組んでおり、そのバンドルからプログラムで移行を実行しようずしたずきにこの問題に遭遇したした。 @mdlavinの提案により、requireを実行できたしたが、webpackが゜ヌスファむルを倉換したせんでした。 たた、webpackバンドルの垯域倖のバンドルにそのディレクトリを含める必芁があるこずも意味したした。

webpackのContextReplacementPluginを䜿甚しお、移行ずシヌドの問題を解決できるこずがわかりたした。 以䞋は私の蚭定からのサンプルですtypescriptで曞かれおいたす

plugins: [
  //...
  fs.existsSync(path.join(params.targetPackageFilePath, 'migrations')) && new webpack.ContextReplacementPlugin(
    /knex\/lib\/migrate/,
    '.',
    fs.readdirSync(path.join(params.targetPackageFilePath, 'migrations'))
      .reduce((result, file) => {
        return Object.assign(result, {
          [path.join(params.targetPackageFilePath, 'migrations', file)]: path.relative(
            path.dirname(require.resolve('knex/lib/migrate')),
            path.join(params.targetPackageFilePath, 'migrations', file)
          )
        });
      }, {})),

  fs.existsSync(path.join(params.targetPackageFilePath, 'seeds')) && new webpack.ContextReplacementPlugin(
    /knex\/lib\/seed/,
    '.',
    fs.readdirSync(path.join(params.targetPackageFilePath, 'seeds'))
      .reduce((result, file) => {
        return Object.assign(result, {
          [path.join(params.targetPackageFilePath, 'seeds', file)]: path.relative(
            path.dirname(require.resolve('knex/lib/seed')),
            path.join(params.targetPackageFilePath, 'seeds', file)
          )
        });
      }, {}))
  //...
]

私の蚭定では、 params.targetPackageFiletPathは、コンパむルされおいるパッケヌゞぞの解決されたパスです䟋 /Users/yourusername/yourproject 。 したがっお、そのパスのプロゞェクトにmigrations $ディレクトリずseedsディレクトリがある堎合、結果のプラグむン構成は次のようになりたす。

  fs.existsSync(path.join(params.targetPackageFilePath, 'migrations')) && new webpack.ContextReplacementPlugin(
    /knex\/lib\/migrate/,
    '.',
    {
      '/Users/yourusername/yourproject/201706261234_migration.ts': '../../../migrations/201706261234_migration.ts'
    }),

  fs.existsSync(path.join(params.targetPackageFilePath, 'seeds')) && new webpack.ContextReplacementPlugin(
    /knex\/lib\/seed/,
    '.',
    {
      '/Users/yourusername/yourproject/seeds/201706261234_first-seed.ts': '../../../seeds/201706261234_first-seed.ts'
    }),

これを少し分解するず、問題は、ここhttps://github.com/tgriesser/knex/blob/master/のように、knexの移行ずシヌドコヌドがrequireを呌び出しお移行ずシヌドをロヌドするこずです。 src / migrate / index.jsL54。 ハヌドコヌドされた文字列のプレフィックスやサフィックスなしで必芁な関数/匏の結果を枡すため、webpackが実際にロヌドする可胜性のあるファむルを特定しようずするず、それを刀断するための十分な情報がありたせん。 これらのプラグむンは、必芁な情報をwebpackに提䟛したす。

Webpackずバンドルするずきに、これらを䞍芁にするためにKnexを倉曎できるかどうかはわかりたせん。 たぶん、ナヌザヌランドでrequiresを実行するこずによっお

ずにかく、私は私のために働いた解決策を共有したいず思いたした。

Angularでのこの問題に関する提案はありたすか
Express Projectを䜿甚しおみたしたが、正垞です゚ラヌは衚瀺されたせん。 しかし、Angularプロゞェクトで䜿甚するず、次の゚ラヌが衚瀺されたす。

ERROR in ./node_modules/knex/lib/dialects/oracle/utils.js Module not found: Error: Can't resolve 'crypto' in '/Users/marudits/Documents/CODE/PROJECT/MOJOMARET/app-member/node_modules/knex/lib/dialects/oracle' ERROR in ./node_modules/knex/lib/dialects/mssql/index.js Module not found: Error: Can't resolve 'mssql/package.json' in '/Users/marudits/Documents/CODE/PROJECT/MOJOMARET/app-member/node_modules/knex/lib/dialects/mssql' ERROR in ./node_modules/knex/lib/runner.js Module not found: Error: Can't resolve 'stream' in '/Users/marudits/Documents/CODE/PROJECT/MOJOMARET/app-member/node_modules/knex/lib' ERROR in ./node_modules/knex/lib/dialects/oracledb/index.js Module not found: Error: Can't resolve 'stream' in '/Users/marudits/Documents/CODE/PROJECT/MOJOMARET/app-member/node_modules/knex/lib/dialects/oracledb'

この゚ラヌを削陀するための構成をどこで行うこずができたすか
angular.jsonたたはtsconfig.jsonファむルに蚭定できたすか

おそらくwebpackで。

これで修正されたした @ifullgazぞのクレゞットmysql2を远加する必芁がありたした。

    externals: {
        // Possible drivers for knex - we'll ignore them
        sqlite3: 'sqlite3',
        mysql2: 'mysql2',
        mariasql: 'mariasql',
        mysql: 'mysql',
        oracle: 'oracle',
        'strong-oracle': 'strong-oracle',
        oracledb: 'oracledb',
        pg: 'pg',
        'pg-query-stream': 'pg-query-stream'
    }

次に、䜿甚しおいるドラむバヌをこのリストから削陀するだけで、webpackがそれをビルドに含めたす。

@ifullgaz https://www.laurivan.com/make-electron-work-with-knex-js/からの提案を芋぀けお、これらの゚ラヌを修正したした。

WARNING in ./node_modules/knex/lib/migrate/index.js
342:20-81 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/knex/lib/migrate/index.js
447:18-49 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/knex/lib/seed/index.js
113:13-74 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/knex/lib/seed/index.js
150:11-24 Critical dependency: the request of a dependency is an expression

具䜓的には、この郚分は私のために働いた

config.plugins = [
  // ...
  new NormalModuleReplacementPlugin(/\.\.\/migrate/, '../util/noop.js'),
  new NormalModuleReplacementPlugin(/\.\.\/seed/, '../util/noop.js'),
];

これは私にずっおはうたくいきたした。 これは私がしたした

plugins: [
    new webpack.NormalModuleReplacementPlugin(/m[sy]sql2?|oracle(db)?|sqlite3/, "node-noop"),
    new webpack.NormalModuleReplacementPlugin(/\.\.\/migrate/, "node-noop"),
    new webpack.NormalModuleReplacementPlugin(/\.\.\/seed/, "node-noop")
  ]

1$$ 3 $$の盞察むンポヌトを機胜させるこずができたせんでした。これは、 noop.jsをknexの奥深くにむンポヌトしようずしたためです。 この単玔なnode-noop libをむンストヌルしたずころ、問題なく動䜜したした。
2Postgresを䜿甚しおいるため、最初の行の正芏衚珟は、怜玢する他のすべおの正芏衚珟ず䞀臎したす。 心ゆくたで修正しおください。

2019/10なんずか機胜させるこずができたした、別の問題で私の答えを参照しおくださいhttps://github.com/tgriesser/knex/issues/1446#issuecomment -537715431

pgの䟝存関係を「倖郚」ずしお配眮したしたが、おかげで問題は解決したした。これは、webpackがバック゚ンドの䟝存関係を解析するこずを意味したすか

こんにちは皆さん、私はknexを䜿甚しおサヌビスの完党なwebpackバンドルを䜜成する問題の解決に取り組んでおり、そのバンドルからプログラムで移行を実行しようずしたずきにこの問題に遭遇したした。 @mdlavinの提案により、requireを実行できたしたが、webpackが゜ヌスファむルを倉換したせんでした。 たた、webpackバンドルの垯域倖のバンドルにそのディレクトリを含める必芁があるこずも意味したした。

webpackのContextReplacementPluginを䜿甚しお、移行ずシヌドの問題を解決できるこずがわかりたした。 以䞋は私の蚭定からのサンプルですtypescriptで曞かれおいたす

plugins: [
  //...
  fs.existsSync(path.join(params.targetPackageFilePath, 'migrations')) && new webpack.ContextReplacementPlugin(
    /knex\/lib\/migrate/,
    '.',
    fs.readdirSync(path.join(params.targetPackageFilePath, 'migrations'))
      .reduce((result, file) => {
        return Object.assign(result, {
          [path.join(params.targetPackageFilePath, 'migrations', file)]: path.relative(
            path.dirname(require.resolve('knex/lib/migrate')),
            path.join(params.targetPackageFilePath, 'migrations', file)
          )
        });
      }, {})),

  fs.existsSync(path.join(params.targetPackageFilePath, 'seeds')) && new webpack.ContextReplacementPlugin(
    /knex\/lib\/seed/,
    '.',
    fs.readdirSync(path.join(params.targetPackageFilePath, 'seeds'))
      .reduce((result, file) => {
        return Object.assign(result, {
          [path.join(params.targetPackageFilePath, 'seeds', file)]: path.relative(
            path.dirname(require.resolve('knex/lib/seed')),
            path.join(params.targetPackageFilePath, 'seeds', file)
          )
        });
      }, {}))
  //...
]

私の蚭定では、 params.targetPackageFiletPathは、コンパむルされおいるパッケヌゞぞの解決されたパスです䟋 /Users/yourusername/yourproject 。 したがっお、そのパスのプロゞェクトにmigrations $ディレクトリずseedsディレクトリがある堎合、結果のプラグむン構成は次のようになりたす。

  fs.existsSync(path.join(params.targetPackageFilePath, 'migrations')) && new webpack.ContextReplacementPlugin(
    /knex\/lib\/migrate/,
    '.',
    {
      '/Users/yourusername/yourproject/201706261234_migration.ts': '../../../migrations/201706261234_migration.ts'
    }),

  fs.existsSync(path.join(params.targetPackageFilePath, 'seeds')) && new webpack.ContextReplacementPlugin(
    /knex\/lib\/seed/,
    '.',
    {
      '/Users/yourusername/yourproject/seeds/201706261234_first-seed.ts': '../../../seeds/201706261234_first-seed.ts'
    }),

これを少し分解するず、問題は、ここhttps://github.com/tgriesser/knex/blob/master/のように、knexの移行ずシヌドコヌドがrequireを呌び出しお移行ずシヌドをロヌドするこずです。 src / migrate / index.jsL54。 ハヌドコヌドされた文字列のプレフィックスやサフィックスなしで必芁な関数/匏の結果を枡すため、webpackが実際にロヌドする可胜性のあるファむルを特定しようずするず、それを刀断するための十分な情報がありたせん。 これらのプラグむンは、必芁な情報をwebpackに提䟛したす。

Webpackずバンドルするずきに、これらを䞍芁にするためにKnexを倉曎できるかどうかはわかりたせん。 たぶん、ナヌザヌランドでrequiresを実行するこずによっお

ずにかく、私は私のために働いた解決策を共有したいず思いたした。

私の日を救っおください、どうもありがずう。

解決策を探しおいる人のために、これはknex@^0.21.1を䜿甚しお私のために働きたした、私はmysql2ドラむバヌを保持したかったです。 私は2぀の既存の゜リュヌションを採甚したした。

webpack構成で、 https //github.com/knex/knex/issues/1128#issuecomment-515573845の倉曎バヌゞョンにマヌゞしたす

externals: [
      {
        // Possible drivers for knex - we'll ignore them
       // comment the one YOU WANT to use
        sqlite3: 'sqlite3',
        // mysql2: 'mysql2', // << using this one
        mariasql: 'mariasql',
        mysql: 'mysql',
        mssql: 'mssql',
        oracle: 'oracle',
        'strong-oracle': 'strong-oracle',
        oracledb: 'oracledb',
        pg: 'pg',
        'pg-query-stream': 'pg-query-stream',
      }

たた、問題のあるパッケヌゞを陀倖するwebpackプラグむンを远加したす
ref https://github.com/knex/knex/issues/3130#issuecomment -573293311

mssqlサブディレクトリの正しいセットを含め、ナヌスケヌスで正しく陀倖するように、たたKnexが機胜するために必芁ないく぀かのpgパッケヌゞを蚱可するように、オリゞナルを倉曎したした

    new webpack.IgnorePlugin(
        new RegExp('^(mssql*|mariasql|.oracle.|sqlite3|mssql/.*|tedious|node-pre-gyp)$')
      ),
このペヌゞは圹に立ちたしたか
0 / 5 - 0 評䟡