【问题标题】:Babel React transform : Property value expected type of string but got nullBabel React 转换:属性值预期的字符串类型但为空
【发布时间】:2016-05-24 02:20:36
【问题描述】:

我有一个运行良好的客户端应用程序(使用 Webpack/Babel 进行转译和编译)。

我正在尝试使用 Node 渲染此应用服务器端,但出现此错误:

TypeError: C:/[PROJECT_PATH]/src/_base/common/components/general/AComponent.js: Property value expected type of string but got null
at Object.validate (C:\[PROJECT_PATH]\node_modules\babel-types\lib\definitions\index.js:153:13)
at validate (C:\[PROJECT_PATH]\node_modules\babel-types\lib\index.js:269:9)
at Object.builder (C:\[PROJECT_PATH]\node_modules\babel-types\lib\index.js:222:7)
at File.<anonymous> (C:\[PROJECT_PATH]\node_modules\babel-core\lib\transformation\file\index.js:329:56)
at File.addImport (C:\[PROJECT_PATH]\node_modules\babel-core\lib\transformation\file\index.js:336:8)
at C:\[PROJECT_PATH]\node_modules\babel-plugin-react-transform\lib\index.js:257:46
at Array.map (native)
at ReactTransformBuilder.initTransformers (C:\[PROJECT_PATH]\node_modules\babel-plugin-react-transform\lib\index.js:255:40)
at ReactTransformBuilder.build (C:\[PROJECT_PATH]\node_modules\babel-plugin-react-transform\lib\index.js:164:41)
at PluginPass.Program (C:\[PROJECT_PATH]\node_modules\babel-plugin-react-transform\lib\index.js:331:17)

它发生在任何导入的组件上。 我知道组件本身可以工作,因为它们在客户端模式下工作。

这是我的 index.js(与客户端应用程序中相同的 babel 配置):

require('babel-register')({
  presets:["es2015", "stage-0",'react'],
  highlightCode: false,
  sourceMaps: "both",
  env: {
    development: {
      plugins: [
        'transform-decorators-legacy',
        ["react-transform", {
          transforms: [{
            imports: ['react'],
            locals: ['module']
          }]
        }]
      ]
    }
  }
});

require('./renderer.js');

这是我的 renderer.js:

import React, { Component } from 'react'

import Router, {match, RoutingContext } from 'react-router'

// this works :
import AnyActions from 'path/to/actions/AnyActions'

// this don't
import AnyComponent from 'path/to/any/component'

其他的都被注释掉了!

我尝试导入这个简单的 AComponent:

import React, { Component, PropTypes } from 'react'

export default class AComponent extends Component {

  render () { return (<p>Hello</p>)}

}

同样的错误!

我必须错过一些明显的东西......但我没有看到它!

【问题讨论】:

  • 您的C:/[PROJECT_PATH]/src/_base/common/containers/InitPage.js 是什么样的?文件抛出错误。
  • 正如我所说,它发生在我尝试导入的任何组件上。我编辑了我的消息以澄清并添加测试“AComponent”组件的代码......如果你能看到一些东西......

标签: javascript node.js reactjs ecmascript-6 babeljs


【解决方案1】:

问题在于我的 Babel 的 React-Transform 配置不正确:我指定了导入和本地变量,但没有任何转换名称......

删除这部分解决了我的问题。 这是我的新 index.js:

require('babel-register')({
  presets:["es2015", "stage-0",'react'],
  highlightCode: false,
  sourceMaps: "both",
  env: {
    development: {
      plugins: [
        'transform-decorators-legacy'
      ]
    }
  }
});

require('./renderer.js');

【讨论】:

    猜你喜欢
    • 2019-02-09
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    相关资源
    最近更新 更多