Javascript 新节点目标的babel7配置在“import”语句上失败
在使用webpack、babel和节点v12.13构建我的Javascript 新节点目标的babel7配置在“import”语句上失败,javascript,node.js,webpack,babeljs,Javascript,Node.js,Webpack,Babeljs,在使用webpack、babel和节点v12.13构建我的节点api时,我遇到以下错误: Module build failed (from /../node_modules/babel-loader/lib/index.js):TypeError: /../src/handler.js: Property name expected type of string but got null 这似乎与源文件中的ES6import语句有关 下面是我最后的配置。 基于此,唯一能让它工作的是设置{ta
节点
api时,我遇到以下错误:
Module build failed (from /../node_modules/babel-loader/lib/index.js):TypeError: /../src/handler.js:
Property name expected type of string but got null
这似乎与源文件中的ES6import
语句有关
下面是我最后的配置。
基于此,唯一能让它工作的是设置{targets:{node:6}}
,但我需要将target设置为v12
我还没有找到相关的讨论。这个问题的原因可能是什么?
这是纱线工作区中的一个包
,但我认为这不重要
.babelrc:(位于api包根目录中,结果与.babelrc.js
或babel.config.json
相同)
webpack.config.js:
const path = require('path')
const nodeExternals = require('webpack-node-externals')
const slsw = require('serverless-webpack')
const webpack = require('webpack')
module.exports = {
entry: slsw.lib.entries,
target: 'node',
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
optimization: {
minimize: false
},
performance: {
hints: false
},
devtool: slsw.lib.webpack.isLocal ? 'eval-source-map' : 'none',
externals: [
nodeExternals({ whitelist: ['workspace-package-b'] }),
nodeExternals({
modulesDir: path.resolve(__dirname, '../../node_modules'),
whitelist: ['workspace-package-b']
})
],
module: {
rules: [
{
test: /\.graphql$/,
loader: 'graphql-tag/loader'
},
{
test: /\.js$/,
exclude: /node_modules/,
use: ['imports-loader?graphql', 'babel-loader']
}
]
},
output: {
libraryTarget: 'commonjs2',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
sourceMapFilename: '[file].map'
},
plugins: [new webpack.DefinePlugin({ 'global.GENTLY': false })]
}
相关package.json:
(dependencies)
"babel-runtime": "^6.26.0",
"core-js": "^3.6.4",
(devDependencies)
"@babel/core": "^7.9.0",
"@babel/plugin-proposal-object-rest-spread": "^7.4.4",
"@babel/plugin-transform-modules-commonjs": "^7.9.0",
"@babel/plugin-transform-regenerator": "^7.8.7",
"@babel/plugin-transform-runtime": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"babel-jest": "24.9.0",
"babel-loader": "8.1.0",
"babel-plugin-source-map-support": "^2.0.1",
"jest": "24.9.0",
"webpack": "4.19.1",
"webpack-node-externals": "^1.7.2"
那种形式的
.babelrc
配置文件已被弃用,而不是使用babeljs推荐的babel.config.js
,如果你想这样称呼它,有一个很酷的特性。你会得到一个param,它有很多你可以使用的技巧
。。。顺便说一句,您缺少@babel/cli、@babel/runtime@babel/core、babel-core@7.0.0-bridge.0
“严格使用”;
module.exports=函数(api){
const{NODE_ENV}=process.ENV;
api.cache(()=>NODE_ENV)
api.env()
如果(节点_环境==“开发”){
api.async();
}
返回{
插件:[],
预设:[]
}
}
那种形式的.babelrc
配置文件已经被弃用了,取而代之的是babeljs推荐的babel.config.js
,如果你想这样称呼它,有一个很酷的特性。你会得到一个param,它有很多你可以使用的技巧
。。。顺便说一句,您缺少@babel/cli、@babel/runtime@babel/core、babel-core@7.0.0-bridge.0
“严格使用”;
module.exports=函数(api){
const{NODE_ENV}=process.ENV;
api.cache(()=>NODE_ENV)
api.env()
如果(节点_环境==“开发”){
api.async();
}
返回{
插件:[],
预设:[]
}
}
原来要点是:
我最初添加这些插件是因为jest抱怨看到了导入
'@babel/plugin-transform-runtime',
'@babel/plugin-transform-regenerator',
'@babel/plugin-transform-modules-commonjs'
但后来我意识到,这打破了我的正常网页建设
与此相关的两个问题似乎是:
- 网页包与以下内容的关系:
到v24的突破性升级(巴别斯jest
假设存在)预设环境
.babelrc.js
为我解决了这个问题:
/*
* webpack 2 offers native support for ES modules. However, Jest runs in Node,
* and thus requires ES modules to be transpiled to CommonJS modules.
*
* Reads:
* https://jestjs.io/docs/en/webpack
* https://github.com/facebook/jest/issues/6229
*
*/
const configByEnv = isTest => {
// https://babeljs.io/docs/en/babel-preset-env#targets
// "Sidenote, if no targets are specified, @babel/preset-env will transform
// all ECMAScript 2015+ code by default."
const targets = isTest ? {} : { node: 'current' }
const addPlugins = isTest
? [
'@babel/plugin-transform-runtime',
'@babel/plugin-transform-regenerator',
'@babel/plugin-transform-modules-commonjs'
]
: []
return {
comments: false,
presets: [['@babel/preset-env', { targets }]],
plugins: [
...addPlugins,
'@babel/plugin-proposal-object-rest-spread',
'source-map-support'
]
}
}
module.exports = api => {
const isTest = api.env('test')
return configByEnv(isTest)
}
事实证明,要点如下: 我最初添加这些插件是因为jest抱怨看到了
导入
'@babel/plugin-transform-runtime',
'@babel/plugin-transform-regenerator',
'@babel/plugin-transform-modules-commonjs'
但后来我意识到,这打破了我的正常网页建设
与此相关的两个问题似乎是:
- 网页包与以下内容的关系:
到v24的突破性升级(巴别斯jest
假设存在)预设环境
.babelrc.js
为我解决了这个问题:
/*
* webpack 2 offers native support for ES modules. However, Jest runs in Node,
* and thus requires ES modules to be transpiled to CommonJS modules.
*
* Reads:
* https://jestjs.io/docs/en/webpack
* https://github.com/facebook/jest/issues/6229
*
*/
const configByEnv = isTest => {
// https://babeljs.io/docs/en/babel-preset-env#targets
// "Sidenote, if no targets are specified, @babel/preset-env will transform
// all ECMAScript 2015+ code by default."
const targets = isTest ? {} : { node: 'current' }
const addPlugins = isTest
? [
'@babel/plugin-transform-runtime',
'@babel/plugin-transform-regenerator',
'@babel/plugin-transform-modules-commonjs'
]
: []
return {
comments: false,
presets: [['@babel/preset-env', { targets }]],
plugins: [
...addPlugins,
'@babel/plugin-proposal-object-rest-spread',
'source-map-support'
]
}
}
module.exports = api => {
const isTest = api.env('test')
return configByEnv(isTest)
}
不幸的是,所有这些并没有改变结果的任何方面。(我们需要什么
babel核心和babel-core@bridge
for?)Reading我不认为。babelrc
真的不受欢迎,但它在6到7之间的行为不同。@bebbi抱歉,我的手指很胖,不需要babel core,只是babel-core@7.0.0-bridge.0
不幸的是,所有这些都不会改变结果的任何方面。(我们需要什么babel核心和babel-core@bridge
for?)阅读我不认为。babelrc
真的不受欢迎,但它在6到7之间的行为不同。@bebbi抱歉,我的手指太胖了,不需要babel core,只需babel即可-core@7.0.0-bridge.0