【问题标题】:Unexpected token 'export' when using webpack使用 webpack 时出现意外的令牌“导出”
【发布时间】:2021-06-22 19:38:00
【问题描述】:

我在 webpack 中的 babel 配置:

module.exports = {
  // Tell webpack to run babel on every file it runs through
  module: {
  rules: [
    {
      test: /\.(js|ts|tsx)?$/,
      loader: 'babel-loader',
      include: [
        path.join(__dirname, 'src'),
        path.join(
          __dirname,
          '..',
          'node_modules/@platformbuilders/react-ui/dist/index',
        ),
      ],
      options: {
        babelrc: false,
        presets: [
          '@babel/react',
          '@babel/preset-typescript',
          [
            '@babel/preset-env',
            {
              loose: true,
              modules: false,
            },
          ],
        ],
        plugins: [
          '@babel/proposal-object-rest-spread',
          ['@babel/plugin-proposal-decorators', { legacy: true }],
          ['@babel/plugin-proposal-class-properties', { loose: true }],
          'dynamic-import-node',
          '@babel/plugin-syntax-dynamic-import',
          '@babel/plugin-transform-runtime',
        ],
      },
    },
    {
      test: /\.html$/,
      use: 'html-loader',
    },
  ],
  },
};

1- webpack --config webpack.server.cjs --mode开发

2- nodemon --watch build --exec node ./build/dist/assets/app.js

这是输出错误:

/mnt/f/Apps/Github/services-portal-landing/node_modules/@platformbuilders/react-ui/dist/index.js:1
export * from './components';
^^^^^^

SyntaxError: Unexpected token 'export'
at wrapSafe (internal/modules/cjs/loader.js:1116:16)
at Module._compile (internal/modules/cjs/loader.js:1164:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Module.require (internal/modules/cjs/loader.js:1089:19)
at require (internal/modules/cjs/helpers.js:73:18)
at Object.@platformbuilders/react-ui (/mnt/f/Apps/Github/services-portal-landing/build/dist/assets/app.js:9240:18)
at __webpack_require__ (/mnt/f/Apps/Github/services-portal-landing/build/dist/assets/app.js:9660:41)
at Object../src/components/index.ts (/mnt/f/Apps/Github/services-portal-landing/build/dist/assets/app.js:5927:84)

这似乎是这个模块的问题@platformbuilders/react-ui/...但我不知道如何解决这个问题。我尝试了很多事情都没有成功... 我正在尝试在节点中渲染 react 项目。

编辑: 这是@platformbuilders/react-ui/dist 中的 tsconfig

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6",
    "module": "es6",
    "lib": ["es6", "DOM"],
    "types": ["react"],
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "dist",
    "importHelpers": true,
    "resolveJsonModule": true,
    "jsx": "react",
    "baseUrl": ".",
    "skipLibCheck": true,
    /* Strict Type-Checking Options */
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "suppressImplicitAnyIndexErrors": true,
    /* Additional Checks */
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    /* Module Resolution Options */
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "compileOnSave": true,
  "include": ["src"],
  "exclude": ["node_modules", "dist"]
}

【问题讨论】:

标签: node.js reactjs webpack babeljs server-side-rendering


【解决方案1】:

您需要启用 ES6 模块的编译:

[
  '@babel/preset-env',
  {
    loose: true,
    modules: true, // <= here
  },
],

您可以在preset documentation 中找到有关此设置的文档。

【讨论】:

  • 更改为“auto”和“commonjs”,但一直输出相同的错误
猜你喜欢
  • 2017-03-05
  • 2018-11-24
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
  • 1970-01-01
  • 2021-06-13
  • 2020-12-23
相关资源
最近更新 更多