简体   繁体   English

在小型节点项目上使用带有 preset-env 的 babel,无法将 import/export 语句转换为 commonjs

[英]Using babel with preset-env on a small node project, unable to convert import/export statements to commonjs

I'm trying to set up a node project so I can use the common import/export syntax, and for compatibility, use babeljs to convert the import and export statements to require s.我正在尝试设置一个节点项目,以便我可以使用通用的导入/导出语法,并且为了兼容性,使用 babeljs 将importexport语句转换为require s。

When I try transpiling using babel though, I'm not seeing any changes and I don't know why.但是,当我尝试使用 babel 进行转译时,我没有看到任何变化,而且我也不知道为什么。

Here's my babel.config.js这是我的babel.config.js

// babel.config.js
module.exports = {
  // I thought this was what I would need to add for making the changes, based on the docs
  plugins: ["@babel/plugin-transform-modules-commonjs"],


  // env takes care of the majority of changes I might need to convert code to make
  // easy to run on a wider range of machines
  presets: [
    [
      "@babel/preset-env",
      {
        targets: {
          // target an older version where I know there is no ESM support
          node: "10"
        }
      }
    ]
  ],
  // this was in the docs too - I'm not sure if  I need it TBH
  sourceType: "module"
}

I have a bunch of code using import/export , and async/await in a directory, that I wanted to transpile and put into another directory, lib .我在一个目录中有一堆使用import/exportasync/await的代码,我想将它们转换并放入另一个目录lib中。

Here's the command I am using:这是我正在使用的命令:

npx babel src --out-dir lib

My package.json file online too, and there's nothing too esoteric there.我的 package.json 文件也在网上,那里没有什么太深奥的。 I've tried to follow the documentation faithfully.我试图忠实地遵循文档。

I would expect to see changes in the code to replace the import/export calls with requires in the lib, but no joy.我希望看到代码发生变化,用库中的 require 替换导入/导出调用,但并不令人高兴。

What am I doing wrong here?我在这里做错了什么? I worked on a related project 6-7 months ago, where I am using babel like this and it does make the changes for me.我在 6-7 个月前从事过一个相关项目,我正在使用这样的 babel,它确实为我做了一些改变。

You can see the branch with the problem code on github , with the package.json , and here's the source code for other project mentioned where it is working .您可以在 github 和package.json上看到问题代码的分支这里提到的其他项目的源代码

Please be gentle, fellow SO commenters - I'm trying my best, I really am. SO 评论员们,请保持温和 - 我正在尽力而为,真的。

It looks like you're using "babel-cli": "^6.26.0" instead of "@babel/cli": "^7.11.6" that should fix it.看起来你正在使用"babel-cli": "^6.26.0"而不是"@babel/cli": "^7.11.6"应该修复它。

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

相关问题 无法使用 @babel/preset-env 运行 node.js 文件 - Unable to run a node.js file with @babel/preset-env 在节点应用程序中使用 babel/preset-env 启用扩展运算符 - Enable spread operator using babel/preset-env in node application 尝试安装 @babel/preset-env 时出错 - error trying to install @babel/preset-env Babel preset-env 未加载节点目标的顶级等待语法 - Babel preset-env not loading top level await syntax for node target 模块构建失败找不到模块'@babel/preset-env' - Module build failed Cannot find module '@babel/preset-env' @babel/preset-env 版本 7 未转换 null-coalescing 运算符 - @babel/preset-env version 7 not transforming the null-coalescing operator 为什么我在使用preset-env节点时无法解析nodejs默认模块? - Why can't I resolve nodejs default modules when using preset-env node? 尝试运行 vue 项目但错误:找不到模块'@vue/cli-plugin-babel' 我安装了依赖项 npm install @babel/core @babel/preset-env - Trying to run vue project but Error: Cannot find module '@vue/cli-plugin-babel' I installed the dependency npm install @babel/core @babel/preset-env 如何在使用 babel 或 webpack 的 ES6 导入/导出模块中使用 Node.js CommonJS 模块? - How can I use Node.js CommonJS modules in ES6 import/export modules using babel or webpack? 如何在package.json文件中配置babel / preset-env? - How do I configure my babel/preset-env in my package.json file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM