[英]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 将
import
和export
语句转换为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/export
和async/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.