代码之家  ›  专栏  ›  技术社区  ›  Xero

专用NPM库引发错误“模块解析失败”

  •  1
  • Xero  · 技术社区  · 5 年前

    我可以在开发时测试我的应用程序 npm run start

    但当我想在其他项目中使用它时,我得到了一个错误:

    ./node\u modules/mylib/src/views/calendar.jsx中出错

    涉及的线路是:

    const Calendar = ({ month = moment(), dayCellTitle = <DayCellTitle />, header = <CalendarHeader/>, children }) => {
    

    "mylib": "git+ssh://git@gitlab.mycompany.ch:mylib/mylib.git"
    

    我尽力了 this ,但最糟糕的是:

    插件/预置文件不允许导出对象,只能导出函数。 在 /home/xxxxx/Documents/workspace/react calendar/node\u modules/babel-preset-stage-0/lib/index.js

    以下是我的文件包:

    webpack.config.js文件

    const path = require('path');
    const pkg = require('./package.json');
    const libraryName= pkg.name;
    module.exports = {
      output: {
        path: path.join(__dirname, './dist'),
        filename: 'react-calendar.js',
        library: libraryName,
        libraryTarget: 'umd',
        publicPath: '/dist/',
        umdNamedDefine: true
      },
      module: {
        rules: [
          { test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            use: ['babel-loader']
          }
        ]
      },
      resolve: {
        alias: {
          'react': path.resolve(__dirname, './node_modules/react'),
          'react-dom': path.resolve(__dirname, './node_modules/react-dom'),
        }
      },
      externals: {
        // Don't bundle react or react-dom
        react: {
          commonjs: "react",
          commonjs2: "react",
          amd: "React",
          root: "React"
        },
        "react-dom": {
          commonjs: "react-dom",
          commonjs2: "react-dom",
          amd: "ReactDOM",
          root: "ReactDOM"
        }
      }
    };
    

    包.json

    {
      "name": "react-calendar",
      "version": "0.0.1",
      "description": "React Calendar UI by",
      "main": "./dist/lib/index.js",
      "scripts": {
        "start": "./node_modules/.bin/parcel docs/index.html",
        "build": "./node_modules/.bin/webpack --mode=production",
        "build:docs": "./node_modules/.bin/parcel build docs/index.js -d ./dist/docs && cp docs/index.html dist/docs/index.html",
        "test": "jest",
        "tdd": "jest --watch"
      },
      "author": "Xero",
      "license": "MIT",
      "dependencies": {
        "lodash": "^4.17.11",
        "mobx": "^5.9.0",
        "mobx-react": "^5.4.3",
        "moment": "^2.24.0",
        "moment-range": "^4.0.1",
        "react": "^16.2.0",
        "react-dom": "^16.2.0",
        "semantic-ui-css": "^2.4.1",
        "semantic-ui-react": "^0.85.0",
        "settings": "^0.1.1"
      },
      "peerDependencies": {
        "lodash": "^4.17.11",
        "mobx": "^5.9.0",
        "mobx-react": "^5.4.3",
        "moment": "^2.24.0",
        "moment-range": "^4.0.1",
        "react": "^16.2.0",
        "react-dom": "^16.2.0",
        "semantic-ui-css": "^2.4.1",
        "semantic-ui-react": "^0.85.0",
        "settings": "^0.1.1"
      },
      "devDependencies": {
        "@babel/core": "^7.0.0-0",
        "@babel/plugin-proposal-class-properties": "^7.3.0",
        "@babel/plugin-proposal-decorators": "^7.3.0",
        "@babel/preset-env": "^7.3.1",
        "@babel/preset-react": "^7.0.0",
        "babel-loader": "^8.0.5",
        "babel-plugin-module-resolver": "^3.1.3",
        "babel-plugin-transform-class-properties": "^6.24.1",
        "babel-preset-env": "^1.7.0",
        "babel-preset-react": "^6.24.1",
        "enzyme": "^3.8.0",
        "enzyme-adapter-react-16": "^1.9.1",
        "jest": "^24.1.0",
        "parcel-bundler": "^1.6.2",
        "react-test-renderer": "^16.8.0",
        "webpack": "^4.1.1",
        "webpack-cli": "^2.0.12",
        "webpack-node-externals": "^1.6.0"
      },
      "jest": {
        "moduleNameMapper": {
          "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/spec/javascript/__mocks__/fileMock.js",
          "\\.(css|scss|less)$": "<rootDir>/spec/javascript/__mocks__/styleMock.js"
        },
        "roots": [
          "src",
          "tests"
        ],
        "moduleFileExtensions": [
          "js",
          "jsx"
        ],
        "moduleDirectories": [
          "node_modules",
          "src"
        ],
        "setupFiles": [
          "<rootDir>/tests/setup.js"
        ]
      }
    }
    

    {
      "presets": ["@babel/preset-env", "@babel/preset-react",],
      "plugins":  [
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        ["@babel/plugin-proposal-class-properties", { "loose": true }]
      ]
    }
    

    附带问题:

    在其他项目中进行导入之前,是否有方法知道库是否会被正确导入

    我补充说 yarn run build

    Entrypoint main [big] = react-calendar.js
     [45] (webpack)/buildin/module.js 497 bytes {0} [built]
     [48] (webpack)/buildin/global.js 472 bytes {0} [built]
    [244] ./node_modules/moment/locale sync ^\.\/.*$ 3 KiB {0} [optional] [built]
    [368] ./src/index.js + 52 modules 182 KiB {0} [built]
          | ./src/index.js 65 bytes [built]
          | ./src/views/calendar.jsx 1.17 KiB [built]
          | ./src/views/day_cell_title.jsx 358 bytes [built]
          | ./src/views/calendar_header.jsx 1.15 KiB [built]
          | ./src/store/calendar_store.js 3.85 KiB [built]
          | ./src/views/calendar_grid.jsx 1.49 KiB [built]
          | ./src/views/calendar_navigation_button.jsx 928 bytes [built]
          | ./src/views/calendar_title.jsx 290 bytes [built]
          | ./src/generators/month_generator.js 556 bytes [built]
          | ./src/painters/day_cell_color.js 593 bytes [built]
          | ./src/views/day_cell_content.jsx 547 bytes [built]
          | ./src/settings.js 332 bytes [built]
          | ./src/painters/event_color.js 374 bytes [built]
          | ./src/painters/range_color.js 591 bytes [built]
          | ./src/views/empty_day_cell_content.jsx 340 bytes [built]
          |     + 38 hidden modules
        + 743 hidden modules
    
    3 回复  |  直到 5 年前
        1
  •  4
  •   Filip Dupanović    5 年前

    您正在重新分发包含JSX语法的源代码。通常,在发布之前,您需要转换这些。如果没有,那么您的家属将需要配置他们的 babel-loader 使用React支持并确保它们不排除您的包模块。

    所以您的配置看起来不错,除了排除匹配:

     module: {
      rules: [
        { test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          use: ['babel-loader']
        }
      ]
     }
    

    我建议你使用 { include } path.dirname(require.resolve('@yours/package-name')) .

        2
  •  2
  •   Xero    5 年前

    1.建造被遗忘而没有承诺的建筑

    我没有(用)建立我的项目 yarn run build ),最重要的是,我没有提交编译后的文件( dist/main.js )

    考虑到第1点,我的观点是错误的。

    import {Calendar} from "project-react-calendar/src/views/calendar";
    

    而不是:

    import {Calendar} from "project-react-calendar";
    

    可以用这个 index.js :

    import { Calendar } from './views/calendar'
    
    export {
        Calendar
    }
    
        3
  •  1
  •   jayarjo    5 年前

    npm run start parcel ,不是 webpack webpack.config.js 根本不影响它?也许你应该确保正确的加载程序确实是由 包裹 .

    "start": "./node_modules/.bin/parcel docs/index.html", // <- invokes parcel bundler
    "build": "./node_modules/.bin/webpack --mode=production",
    

    尝试 npm run build 如果成功的话,情况就是这样。