【问题标题】:webpack: import + module.exports in the same module caused errorwebpack: import + module.exports 在同一个模块中导致错误
【发布时间】:2017-07-15 22:56:43
【问题描述】:

我正在使用 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 有什么问题吗?

【问题讨论】:

标签: javascript import webpack ecmascript-6 require


【解决方案1】:

在我的 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

【讨论】:

    【解决方案2】:

    您可以将要求与导出一起使用。但不是 import 和 module.exports。

    【讨论】:

      【解决方案3】:

      如果下游的其他模块有意外的需求树,就会发生这种情况。 Babel 更改需要导入不应该导入的位置,这会导致上述问题@Matthew Herbst。要解决此问题,请将 "sourceType": "unambiguous" 添加到您的 babelrc 文件或 babel.config.js 中,以便 @babel/plugin-transform-runtime 不会对 require 表达式进行此更改以导入您的 commonjs 文件。例如:

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

      【讨论】:

      • 谢谢。但是它对于现代浏览器来说是一个很好的解决方案,它不会在 IE11 或更低版本上加载
      • 希望这是被接受的答案,而不是要求删除 module.exports 的答案,因为将 CommonJS 模块更改为 ES6 并不总是一种选择。感谢您的解决方案!
      【解决方案4】:

      您不能混合使用 importmodule.exports。在import 的世界里,你需要导出东西。

      // Change this
      module.exports = foo;
      
      // To this
      export default foo;
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-24
      • 2017-07-28
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多