[英]Babel Not Working Outside of Main Node.js App
My Express.js server is currently being started by running start.js
, which in turn runs server.js
(code below) .我的 Express.js 服务器目前正在通过运行
start.js
,而start.js
又运行server.js
(下面的代码) 。 For example,例如,
nodemon start.js
Problem: When I try to run the file at src/lib/seed_database.js
using问题:当我尝试使用
src/lib/seed_database.js
运行文件时
node seed_database.js
we get the errors showing that babel is not transpiling any JS files, such as我们得到的错误表明 babel 没有转译任何 JS 文件,例如
SyntaxError: Unexpected token 'export'
语法错误:意外的标记“导出”
What is the recommended way to get babel to work on a file other that server.js
?让 babel 处理除
server.js
的文件的推荐方法是什么?
Environment环境
Directory Structure目录结构
my-app
├── .node_modules
├── src
│ ├── lib
│ │ └── seed_database.js
│ ├── v1
│ │ └── routes.js
│ ├── start.js
│ └── server.js
├── .babelrc
├── package-lock.json
├── package.json
start.js开始.js
require("@babel/register")({
presets: [
["@babel/preset-env", {
"targets": {
"node": "current"
}
}]
]
});
module.exports = require('./server.js')
server.js服务器.js
import express from 'express';
import v1ApiRoutes from './v1/routes';
import constants from './config/constants';
const app = express();
app.use('/v1', v1ApiRoutes);
app.listen(constants.PORT, err => {
if (err) {
console.log('Cannot run!')
} else {
console.log(`App listening on port: ${constants.PORT}`)
}
});
seed_database.js种子数据库.js
const db = require('../../config/db')['appDb'];
...
db.js数据库.js
...
export const appDb = appDb;
.babelrc .babelrc
{
"plugins": [
["@babel/transform-runtime"]
]
}
Jeremy Harris' suggestion works:杰里米哈里斯的建议有效:
require("@babel/register")({
presets: [
["@babel/preset-env", {
"targets": {
"node": "current"
}
}]
]
});
const db = require('../../config/db')['appDb'];
However, when we replace the require
line with named imports:但是,当我们用命名导入替换
require
行时:
import { appDb as db } from '../../config/db';
we now get a new error我们现在得到一个新的错误
SyntaxError: Cannot use import statement outside a module
语法错误:不能在模块外使用导入语句
Using Jeremy Harris' second suggestion of a single entry point works使用 Jeremy Harris 的第二个单一入口点建议有效
node start.js seed
start.js开始.js
require("@babel/register")({
presets: [
["@babel/preset-env", {
"targets": {
"node": "current"
}
}]
]
});
if (process.argv.indexOf('seed') > -1) {
module.exports = require('./lib/seed_database.js')
return
}
module.exports = require('./server.js')
As we discussed in the comments, the issue is that your seed file was not including the @babel/register
code, hence it didn't transpile it.正如我们在评论中所讨论的,问题是您的种子文件不包含
@babel/register
代码,因此它没有转译它。 Per the suggestion with CLI args, you could modify it to use a single module.exports
like:根据 CLI args 的建议,您可以修改它以使用单个
module.exports
例如:
// Default module
let commandModule = './server.js';
// Override with different command
if (process.argv.indexOf('seed') > -1) {
commandModule = './lib/seed_database.js';
}
module.exports = require(commandModule);
If you want to keep configuration in .babelrc, you can do this when including @babel/register:如果你想在 .babelrc 中保留配置,你可以在包含 @babel/register 时这样做:
require('@babel/register')({
extends: './.babelrc',
ignore: [/node_modules/],
});
This GitHub issue discusses some of the issues around it in case you run into any: https://github.com/babel/babel/issues/8321这个 GitHub 问题讨论了一些围绕它的问题,以防你遇到任何问题: https : //github.com/babel/babel/issues/8321
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.