Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何与monorepo(yarnpkg工作区)一起使用webpack_Node.js_Express_Webpack_Yarnpkg - Fatal编程技术网

Node.js 如何与monorepo(yarnpkg工作区)一起使用webpack

Node.js 如何与monorepo(yarnpkg工作区)一起使用webpack,node.js,express,webpack,yarnpkg,Node.js,Express,Webpack,Yarnpkg,我使用的是根目录中包含所有repo的包目录。每个repo都有自己的包含其依赖项的node_modules目录。根节点_modules目录包含整个项目的所有开发依赖项,以及所有其他与开发相关的内容,如webpack.config文件。Webpack对express server软件包使用热模块重新加载 我遇到的问题是,如何配置webpack externals以排除整个项目中的所有node_模块目录,而不仅仅是根目录 在这种情况下似乎不起作用 错误消息: WARNING in ./packages

我使用的是根目录中包含所有repo的包目录。每个repo都有自己的包含其依赖项的node_modules目录。根节点_modules目录包含整个项目的所有开发依赖项,以及所有其他与开发相关的内容,如webpack.config文件。Webpack对express server软件包使用热模块重新加载

我遇到的问题是,如何配置webpack externals以排除整个项目中的所有node_模块目录,而不仅仅是根目录

在这种情况下似乎不起作用

错误消息:

WARNING in ./packages/servers/express/node_modules/colors/lib/colors.js
127:29-43 Critical dependency: the request of a dependency is an expression

WARNING in ./packages/servers/express/node_modules/express/lib/view.js
79:29-41 Critical dependency: the request of a dependency is an expression
网页包配置:

const webpack = require('webpack');
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const StartServerPlugin = require('start-server-webpack-plugin');

module.exports = {
  entry: [
    'babel-polyfill',
    'webpack/hot/poll?1000',
    path.join(__dirname, '../packages/servers/express/server/index.js')
  ],
  watch: true,
  target: 'node',
  externals: [
    nodeExternals({
      whitelist: ['webpack/hot/poll?1000']
    })
  ],
  resolve: {
    alias: {
      handlebars: 'handlebars/dist/handlebars.js'
    }
  },
  module: {
    rules: [
      {
        test: /\.js?$/,
        use: 'babel-loader',
        exclude: /node_modules/
      }
    ]
  },
  plugins: [
    new StartServerPlugin('server.js'),
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.DefinePlugin({
      'process.env': { BUILD_TARGET: JSON.stringify('server') }
    })
  ],
  output: {
    path: path.join(__dirname, '../packages/servers/express/.build'),
    filename: 'server.js'
  }
};

纱线工作区将兼容模块提升到根节点_模块目录,将任何不兼容(不同的semver等)模块保留在从属工作区的节点_模块目录中。如果在不使用相对路径的情况下请求包,则该包可能是本地的、来自节点_模块的,也可能是来自某个工作区的符号链接包。您可能希望所有这些包都是外部的

externals: [
    (ctx, req, cb) => {
        if (!/node_modules/.test(ctx) && req[0] !== '.') {
            // Assumes you have defined an "entries" variable
            let notAnEntry = (path) => {
                return Object.keys(entries).every((entry) => {
                    return entries[entry] !== path
                });
            };

            if (notAnEntry(require.resolve(req))) {
                // This module is external in a commonjs context
                return cb(null, `commonjs ${req}`);
            }
        }

        cb();
    }
]
如何配置webpack externals以排除整个项目中的所有node_模块目录,而不仅仅是根目录中的目录

我会试试看。系统将向您传递require的上下文、请求的模块名称以及一个回调,以指示是否应将此特定导入(require)视为外部导入

externals: [
    (ctx, req, cb) => {
        if (!/node_modules/.test(ctx) && req[0] !== '.') {
            // Assumes you have defined an "entries" variable
            let notAnEntry = (path) => {
                return Object.keys(entries).every((entry) => {
                    return entries[entry] !== path
                });
            };

            if (notAnEntry(require.resolve(req))) {
                // This module is external in a commonjs context
                return cb(null, `commonjs ${req}`);
            }
        }

        cb();
    }
]

多亏了@blackxored,我才能够在我的项目中修复它

在网页包配置文件中,执行以下操作:

import nodeExternals from 'webpack-node-externals'
然后加上

externals: [
  nodeExternals({
    modulesFromFile: true,
  }),
],

如果将纱线工作区与
webpack节点外部一起使用
比设置
modulesFromFile:true
更好的解决方案是在您的webpack配置中使用以下
externals
设置:

externals: [
  nodeExternals(),
  nodeExternals({
    modulesDir: path.resolve(__dirname, 'path/to/root/node_modules'),
  }),
],

基本上使用两个nodeExternals实例。1个用于包
node\u modules
,一个用于根
node\u modules

工作区的标题拼写错误。我尝试了两种方法,这种方法在构建时间上大约快了1秒(我使用的是一个非常弱的chromebook)。我不确定moduleFromFiles做了什么,但至少通过我有限的测试,这个解决方案速度更快。在搜索了9个小时后,你救了我的命。。。我不需要第一个
nodeExternals()
但是如果我做对了,第一个
nodeExternal()
只会删除在本地节点模块中找到的包,所以在工作区的上下文中几乎什么都不需要。第二个也将删除根节点_模块。找到了将其放在公共网页包配置中的模式:
externals:[nodeExternals({additionalModuleDirs:[path.resolve(u dirname,path/to/root/node_modules”)]]
。它的工作原理与您的答案类似,但additionalModuleDirs更简洁。链接:您能详细说明这一点的后果吗?如果我没有弄错的话,默认的nodeExternals将排除它在根节点_模块和您的代码中找到的任何内容。但是,使用此选项,您会强制他在中阅读您的本地package.json斯蒂德,我说得对吗?