簡體   English   中英

webpack: import + module.exports 在同一個模塊中導致錯誤

[英]webpack: import + module.exports in the same module caused error

我正在用 webpack 開發一個網站。 當我有這樣的代碼時:

import $ from 'jquery';
function foo() {};
module.exports = foo;

我收到錯誤Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

事實證明,將import $ from 'jquery'更改為var $ = require('jquery')不會導致任何錯誤。

為什么使用 module.exports 導入會導致此錯誤? 使用 require 有什么問題嗎?

你不能混合importmodule.exports import世界里,你需要出口東西。

// Change this
module.exports = foo;

// To this
export default foo;

如果下游的其他模塊有意外的需求樹,就會發生這種情況。 Babel 更改需要導入它不應該導入的位置,這會導致上述問題@Matthew Herbst。 要解決這個問題,請將"sourceType": "unambiguous"添加到您的babelrc文件或 babel.config.js 中,這樣@babel/plugin-transform-runtime 就不會在您的 commonjs 文件中導入 require 表達式。 例如:

module.exports = {
  presets: [
    '@quasar/babel-preset-app'
  ],

  "sourceType": "unambiguous"
}

您可以將 require 與 export 一起使用。 但不是 import 和 module.exports。

在我的 react-native-web 案例中,只需使用額外的 webpack 規則,然后 TypeError: Cannot assign to read only property 'exports' of object 是固定的。 也許你可以參考它。

npm install --save-dev react-app-rewired

在你的項目根目錄中創建一個config-overrides.js

// used by react-app-rewired

const webpack = require('webpack');
const path = require('path');

module.exports = {
  webpack: function (config, env) {
    config.module.rules[1].use[0].options.baseConfig.extends = [
      path.resolve('.eslintrc.js'),
    ];

    // To let alias like 'react-native/Libraries/Components/StaticRenderer'
    // take effect, must set it before alias 'react-native'
    delete config.resolve.alias['react-native'];
    config.resolve.alias['react-native/Libraries/Components/StaticRenderer'] =
      'react-native-web/dist/vendor/react-native/StaticRenderer';
    config.resolve.alias['react-native'] = path.resolve(
      'web/aliases/react-native',
    );

    // Let's force our code to bundle using the same bundler react native does.
    config.plugins.push(
      new webpack.DefinePlugin({
        __DEV__: env === 'development',
      }),
    );

    // Need this rule to prevent `Attempted import error: 'SOME' is not exported from` when `react-app-rewired build`
    // Need this rule to prevent `TypeError: Cannot assign to read only property 'exports' of object` when `react-app-rewired start`
    config.module.rules.push({
      test: /\.(js|tsx?)$/,
      // You can exclude the exclude property if you don't want to keep adding individual node_modules
      // just keep an eye on how it effects your build times, for this example it's negligible
      // exclude: /node_modules[/\\](?!@react-navigation|react-native-gesture-handler|react-native-screens)/,
      use: {
        loader: 'babel-loader',
      },
    });

    return config;
  },
  paths: function (paths, env) {
    paths.appIndexJs = path.resolve('index.web.js');
    paths.appSrc = path.resolve('.');
    paths.moduleFileExtensions.push('ios.js');
    return paths;
  },
};

還要創建一個web/aliases/react-native/index.js

// ref to https://levelup.gitconnected.com/react-native-typescript-and-react-native-web-an-arduous-but-rewarding-journey-8f46090ca56b

import {Text as RNText, Image as RNImage} from 'react-native-web';
// Let's export everything from react-native-web
export * from 'react-native-web';

// And let's stub out everything that's missing!
export const ViewPropTypes = {
  style: () => {},
};
RNText.propTypes = {
  style: () => {},
};
RNImage.propTypes = {
  style: () => {},
  source: () => {},
};

export const Text = RNText;
export const Image = RNImage;
// export const ToolbarAndroid = {};
export const requireNativeComponent = () => {};

現在你可以運行react-app-rewired start而不是react-scripts start

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM