【问题标题】:babel-node with typescript throws "Cannot use import statement outside a module" in command line带有打字稿的 babel-node 在命令行中抛出“无法在模块外使用导入语句”
【发布时间】:2022-11-24 03:02:22
【问题描述】:

所以,我看到了很多类似的问题,但大多数都是指构建代码,而这个实际上是一个 CLI 脚本。

我的命令是: node_modules/.bin/babel-node -x .js,.jsx,.ts,.tsx scripts/database/index.ts generate

如果它从node_modules(准确地说是React Native相关)调用一些代码,它会抛出错误。

我试过 type: module 但它导致了更严重的错误。

babel.config.js:

module.exports = {
  presets: [
    'module:metro-react-native-babel-preset',
    '@babel/preset-flow',
  ],
  plugins: [
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    ['@babel/plugin-transform-flow-strip-types'],
    [
      require.resolve('babel-plugin-module-resolver'),
      {
        root: ['.'],
        extensions: [
          '.ios.js',
          '.android.js',
          '.js',
          '.ts',
          '.tsx',
          '.json',
          // '.png',
        ],
        alias: {
          app: ['./app'],
          'test/*': ['test/'],
          '@components': './app/components',
        },
      },
    ],
  ],
  sourceMaps: true,
};

tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "isolatedModules": true,
    "jsx": "react",
    "lib": ["ES2016"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "strict": true,
    "target": "esnext",
    "baseUrl": "./",
    "paths": {
      "app/*": ["app/*"],
      "tests": ["tests/*"],
    },
    // ensure ignores node_modules
    "skipLibCheck": true,
    "preserveSymlinks": true,
    "typeRoots": ["./node_modules/@types"],
    "types": [
      "node",
      "@wdio/types",
      "webdriverio/async",
      // "@wdio/jasmine-framework",
      // "expect-webdriverio/jasmine",
      "jest"
    ]
  },
  // "include": [
  //   "src/*",
  //   "tests/*",
  // ],
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js",
    // isnores special cases
    "**/modules/**",
    "node_modules/react-native/**",
    "node_modules/@react-navigation/**",
  ]
}

【问题讨论】:

    标签: node.js typescript babeljs


    【解决方案1】:

    删除"type": "module"

    安装@babel/register

    在您的第一行代码中,添加这一行
    require('@babel/register')({ extensions: ['.js', '.jsx', '.ts', '.tsx'] })

    npm脚本
    "start": "babel-node -x js,.jsx,.ts,.tsx -- src/app.ts"

    更新:

    结果你可以跳过整个@babel/register,你缺少的是命令中的--

    【讨论】:

    • 我终于有时间尝试了,但遗憾的是当我使用import 语句时它触发了Cannot use import statement outside a module。我也尝试添加 "type": "module" 回来,但它无法识别 .ts 扩展......
    【解决方案2】:

    哇,这太粗糙了。

    我最近开始逐渐在我的 Node 应用程序中采用 TS,我认为这就是原因。

    但我开始认为babel-node 中的--config 的默认选项不再寻找babel.config.js,而是只寻找babel.config.json

    我煞费苦心地将我的 babel 配置转换为 JSON,然后似乎固定。

    另外,--extensions 论点必须包括.ts。因此,要在同一项目中同时支持.js.ts 文件,您需要定义以下内容:

    package.json

    "scripts": {
      "task": "babel-node --extensions .js,.ts -- ",
    }
    

    babel.config.json

    {
      "presets": [
        "@babel/preset-typescript",
        [
          "@babel/preset-env",
          {
            "targets": {
              "node": "current"
            }
          }
        ]
      ],
      "plugins": [
        [
          "module-resolver",
          {
            "root": ["./"],
            "alias": {
              "test": "./__test__"
            }
          }
        ]
      ]
    }
    
    

    最后我能够像这样执行 CLI 命令:

    yarn task something.js

    或者

    yarn task something.ts

    【讨论】:

      猜你喜欢
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2020-12-06
      • 2020-06-05
      • 2018-06-10
      相关资源
      最近更新 更多