简体   繁体   English

带有 typescript 的 babel-node 在命令行中抛出“不能在模块外使用导入语句”

[英]babel-node with typescript throws "Cannot use import statement outside a module" in command line

So, I saw many similar issues, but most of them refer to built code, and this one is actually a CLI script.所以,我看到了很多类似的问题,但大多数都是指构建代码,而这个实际上是一个 CLI 脚本。

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

And if it calls some code from node_modules ( React Native related to be precise) it will throw the error.如果它从node_modules (准确地说是React Native相关)调用一些代码,它将抛出错误。

I tried type: module but it caused even worse errors.我试过type: module但它导致了更严重的错误。

babel.config.js: 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: 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/**",
  ]
}

remove "type": "module"删除"type": "module"

install @babel/register安装@babel/register

in your very first line of code, add this line在您的第一行代码中,添加这一行
require('@babel/register')({ extensions: ['.js', '.jsx', '.ts', '.tsx'] })

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

update:更新:

turn out that you can skip the whole @babel/register thing, what you are missing is a -- in your command原来你可以跳过整个@babel/register事情,你缺少的是--在你的命令

Woof this was rough.哇,这太粗糙了。

I recently started gradually adopting TS into my Node app and I thought that was the cause.我最近开始逐渐在我的 Node 应用程序中采用 TS,我认为这就是原因。

But I'm starting to think the default option for the --config in babel-node is no longer looking for babel.config.js and rather only looking for babel.config.json .但我开始认为babel-node--config的默认选项不再寻找babel.config.js而只是寻找babel.config.json

I painstakingly converted my babel config to JSON and that seems to have fixed.我煞费苦心地将我的 babel 配置转换为 JSON,这似乎已经修复。

Also, the --extensions argument must include .ts .此外,-- --extensions参数必须包含.ts So to support both .js and .ts files in the same project you need to define this:因此,要在同一项目中同时支持.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__"
        }
      }
    ]
  ]
}

Then finally I was able to execute CLI commands like so:最后我能够像这样执行 CLI 命令:

yarn task something.js

or或者

yarn task something.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能在模块 babel 之外使用 import 语句 - Cannot use import statement outside a module babel TypeScript:无法在模块外使用导入语句 (Node.js) - TypeScript: Cannot use import statement outside a module (Node.js) 无法在配置了 Babel 的模块外部使用 import 语句 - Cannot use import statement outside a module with Babel configured 找不到模块表达Babel节点 - Cannot find module express Babel-node 使用 ts-node 执行 typescript 代码会出现“无法在模块外使用导入语句”错误 - Executing typescript code using ts-node gives “Cannot use import statement outside a module” error Jest with TypeScript (Backend - Node/Express) 说 Cannot use import statement outside a module - Jest with TypeScript (Backend - Node/Express) says Cannot use import statement outside a module 不能在模块、Typescript、WebdriverIO 之外使用导入语句 - Cannot Use Import Statement Outside Module, Typescript, WebdriverIO Mocha + TypeScript:不能在模块外使用导入语句 - Mocha + TypeScript: Cannot use import statement outside a module Typescript,反应,Electron:不能在模块外使用导入语句 - Typescript, React, Electron: Cannot use import statement outside a module 不能在模块外使用 import 语句 Electron React Typescript - Cannot use import statement outside a module Electron React Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM