尝试在本地使用自定义构建的模块时收到“错误:无法解析<custom_module_name>”

时间:2019-04-09 19:05:23

标签: webpack npm-install node-modules package.json npm-link

我正在尝试导入我在本地创建并链接/安装到通过webpack构建和运行的另一个模块的模块。

我已经使用了多种方法将自定义模块集成到另一个模块中:

  1. npm link定制模块以创建符号链接,该符号链接显示在另一个模块的node_modules目录中
  2. npm install <local_absolute_path>要安装的自定义模块
  3. 使用wmlhttps://github.com/wix/wml)将自定义模块中已更改的文件复制到node_modules
  4. 将自定义模块发布到npm,然后将其安装在另一个模块中

所有这些方法仍然导致webpack和Flow显示Cannot resolve错误

NPM版本:6.9.1-next.0 节点版本:8.8.1

以下是自定义模块的package.json文件:

{
  "name": "custom_module_name",
  "version": "2.3.0",
  "repository": {
    "type": "git",
    "url": "-----"
  },
  "license": "UNLICENSED",
  "engines": {
    "node": ">=10.10.0",
    "npm": ">=6.4.1"
  },
  "ava": {
    "files": [
      ".test/**/*.js"
    ],
    "require": [
      "./test/helpers/config.js",
      "./test/helpers/utils.js"
    ],
    "babel": {
      "testOptions": {
        "babelrc": false
      }
    },
    "sources": [
      ".dist/**/*"
    ],
    "serial": true,
    "verbose": true,
    "failFast": false,
    "color": true,
    "concurrency": 1,
    "failWithoutAssertions": false,
    "tap": false,
    "timeout": "180s"
  },
  "nightmare": {
    "doc": true,
    "show": true,
    "openDevTools": {
      "mode": "detach"
    },
    "executionTimeout": 10000,
    "waitTimeout": 120000,
    "webPreferences": {
      "partition": "nopersist"
    },
    "switches": {
      "ignore-certificate-errors": true
    },
    "show_console_logging": false
  },
  "glslify": {
    "transform": [
      "glslify-import"
    ]
  }
}

这是另一个试图导入自定义模块的模块的webpack.common.js文件:

const webpack = require('webpack');
const path = require('path');
const glob = require('glob');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const FlowWebpackPlugin = require('flow-webpack-plugin');

module.exports = {
  entry: {
    vendor: ['babel-polyfill', 'react', 'react-dom'],
    annotationService: glob.sync('./ClientScripts/---/*.js'),
    sass: './sass/main.scss'
  },
  output: {
    path: path.join(__dirname, 'reactDist'),
    filename: 'js/[name].js',
    publicPath: '/---/',
    sourceMapFilename: 'map/[name].map'
  },
  optimization: {
    runtimeChunk: 'single',
    splitChunks: {
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors',
          chunks: 'all'
        }
      }
    }
  },
  resolve: {
    alias: {
      Interfaces: path.resolve(__dirname, 'ClientScripts/Interfaces/'),
      "custom_module_name": path.resolve(__dirname, 'node_modules/custom_module_name/')
    },
    symlinks: false
  },
  target: 'web',
  node: {
    fs: "empty"
  },
  externals: {
    'winston': 'require("winston")' // https://stackoverflow.com/questions/37840566/how-do-i-get-winston-to-work-with-webpack/37841103
  },
  module: {
    rules: [
      { test: /\.js$/, loader: 'babel-loader', exclude: [/node_modules/] },
      { test: /\.jsx$/, loader: 'babel-loader', exclude: [/node_modules/] },
      { test: /\.env$/, loader: "file-loader?name=index.[ext]", exclude: [/node_modules/] },
      {
        test: /\.scss$|\.css$/,
        exclude: /node_modules/,
        loader: ExtractTextPlugin.extract({
          use: [{
            loader: "css-loader",
            options: {
              minimize: true
            }
          },'sass-loader']
        })
      },
      { test: /\.(jpe?g|png|gif|svg)$/,
        loader: 'file-loader?name=img/[name].[ext]?',
        options: {
          name (file) {
            if (process.env.environment === 'prod') {
              return '[path][name].[hash].[ext]'
            }

            return '[path][name].[ext]'
          }
        }
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin({ filename: 'css/timeline.[md5:contenthash:hex:20].css', disable: false, allChunks: true }),
    new FlowWebpackPlugin()
  ]
}

这是我的.flowconfig

[ignore]
.*/node_modules/flow-webpack-plugin/.*
.*/node_modules/custom-module-name/.*
.*/node_modules/custom-module-name/**/test/.*
.*/node_modules/.*\.json$
.*/node_modules/\.staging/.*

[libs]
flow-typed

[options]
module.name_mapper='^Interfaces\/\(.*\)$' -> '<PROJECT_ROOT>/ClientScripts/Interfaces/\1'
module.file_ext=.js
module.file_ext=.jsx
module.file_ext=.svg
module.file_ext=.json

我经常收到的两个错误是:

ERROR in ./ClientScripts/-/DashboardGrid.jsx
Module not found: Error: Can't resolve 'custom_module_name' in '-/ClientScripts/-/components'

我认为它来自webpack

ERROR in Flow validation
Error ------------------------------------------ ClientScripts/-/DashboardGrid.jsx:11:22

Cannot resolve module custom_module_name.

      8| const ReactGridLayout = WidthProvider(RGL);
      9|
     10| // AKDV
     11| import { Test } from 'custom_module_name';

来自Flow

我假设问题出在自定义模块设置本身...知道这可能是什么问题吗

0 个答案:

没有答案