Javascript 节点\模块/esnext和commonjs中的syntaxerror意外令牌导出不兼容
在我基于typescript的项目A中,我使用以下方法导入项目B:Javascript 节点\模块/esnext和commonjs中的syntaxerror意外令牌导出不兼容,javascript,node.js,typescript,webpack,ecmascript-6,Javascript,Node.js,Typescript,Webpack,Ecmascript 6,在我基于typescript的项目A中,我使用以下方法导入项目B: 从'@something/projectB'导入{function,functionB} 但我得到了一个错误: Path_To_Project_B\lib\index.js:1 (function (exports, require, module, __filename, __dirname) { export * from './core/index';
从'@something/projectB'导入{function,functionB}
但我得到了一个错误:
Path_To_Project_B\lib\index.js:1
(function (exports, require, module, __filename, __dirname) { export * from './core/index';
^^^^^^
SyntaxError: Unexpected token export
我检查了两个项目的tsconfig.json,下面是项目B的tsconfig.json:
{
"compilerOptions": {
"incremental": true,
"target": "es5",
"module": "esnext",
"jsx": "react",
"declaration": true,
"sourceMap": true,
"experimentalDecorators": true,
"noEmitOnError": true,
"moduleResolution": "node",
"strict": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true
}
}
下面是我的项目A的tsconfig.json:
{
"compileOnSave": true,
"compilerOptions": {
"outDir": "../../dist/ProjectA/dts",
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"lib": [
"dom",
"es6",
"scripthost",
"es2016",
"es2015",
"es2015.promise",
"esnext"
],
"declaration": true,
"sourceMap": true,
"noEmitOnError": true,
"noImplicitThis": true,
"alwaysStrict": true,
"strictBindCallApply": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"experimentalDecorators": true,
},
"include": [
"./scripts/Definitions/*.d.ts",
"./scripts/Main.ts"
],
"exclude": []
}
我尝试过添加babel依赖项和.babelrc(遵循babel官方网站的步骤)
我注意到有一些帖子与Jest相关,但在我的项目A中我没有使用它,而在项目B中,Jest正在使用
我还尝试在我的网页文件中添加“babel loder”:
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: /node_modules/,
use: [
'thread-loader',
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
configFile: path.resolve(
__dirname,
"./.babelrc",
),
},
},
],
enforce: 'pre',
query: {
presets: ['preset-env']
}
},
{
test: /\.ts$/,
loader: 'babel-loader',
include: /node_modules/,
use: [
'thread-loader',
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
configFile: path.resolve(
__dirname,
"./.babelrc",
),
},
},
],
query: {
presets: ['preset-env']
},
use: [
{ loader: "ts-loader", options: tsLoaderOptions },
{ loader: "ifdef-loader", options: ifDefLoaderOptions }
]
}
]
},
但问题仍然存在,如何传输节点_模块下的项目B并供我的项目A使用?谢谢
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: /node_modules/,
use: [
'thread-loader',
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
configFile: path.resolve(
__dirname,
"./.babelrc",
),
},
},
],
enforce: 'pre',
query: {
presets: ['preset-env']
}
},
{
test: /\.ts$/,
loader: 'babel-loader',
include: /node_modules/,
use: [
'thread-loader',
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
configFile: path.resolve(
__dirname,
"./.babelrc",
),
},
},
],
query: {
presets: ['preset-env']
},
use: [
{ loader: "ts-loader", options: tsLoaderOptions },
{ loader: "ifdef-loader", options: ifDefLoaderOptions }
]
}
]
},