webpack:import + module.exports在同一模块中导致错误

时间:2017-02-24 23:30:37

标签: javascript import webpack ecmascript-6 require

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

4 个答案:

答案 0 :(得分:38)

您无法混合importmodule.exports。在import世界中,您需要导出内容。

// Change this
module.exports = foo;

// To this
export default foo;

答案 1 :(得分:1)

您可以在导出中使用require。但不能导入和module.exports。

答案 2 :(得分:1)

在我的react-native-web情况下,只需使用其他webpack规则,即可修复TypeError:无法分配为只读对象的属性“ exports”。也许您可以参考它。

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

答案 3 :(得分:0)

如果下游的其他模块具有意外的需求树,则会发生这种情况。通天塔的变化要求在不应该导致上述问题@Matthew Herbst的地方导入。要解决此问题,请在您的"sourceType": "unambiguous"文件或babel.config.js中添加babelrc,以使@ babel / plugin-transform-runtme不会进行此require表达式更改,以将其导入commonjs文件中。例如:

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

  "sourceType": "unambiguous"
}