Javascript 设置webpack以在最终用户应用程序中需要文件

Javascript 设置webpack以在最终用户应用程序中需要文件,javascript,node.js,webpack,Javascript,Node.js,Webpack,如果我正在开发这样的节点模块: // index.js in library const loadRoutes = () => require('./routes.js') export default loadRoutes 然后使用webpack将其捆绑: // webpack.config.js module.exports = { entry: './index.js', output: { path: path.resolve(__dirname, 'dist')

如果我正在开发这样的节点模块:

// index.js in library
const loadRoutes = () => require('./routes.js')
export default loadRoutes
然后使用webpack将其捆绑:

// webpack.config.js
module.exports = {
  entry: './index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index.js',
    libraryTarget: 'commonjs2'
  },
  mode: 'development'
}
然后在最终用户应用程序中,当我导入库时,我希望
require('./routes.js')
相对于应用程序要求
routes.js

import loadRoutes from 'myLib'
loadRoutes()
但是它抛出了一个异常:
找不到模块'/routes.js'

我试着用。但它没有按我预期的方式工作。网页包配置为:

module.exports = {
  entry: './index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index.js',
    libraryTarget: 'commonjs2'
  },
  plugins: [
    new webpack.IgnorePlugin(/routes\.js/)
  ],
  mode: 'development'
}

我不知道为什么这么有用的东西在webpack中这么难实现。我做错了吗?或者什么?

你试过非网页要求吗?你试过非网页要求吗?