我有一个具有以下结构的项目
./
├── README.md
├── babel.config.js
├── package.json
├── .yarnrc.yml
├── packages
│ ├── pkg-1
│ │ ├── babel.config.js
│ │ ├── index.js
│ │ └── package.json
│ └── pkg-2
│ ├── babel.config.js
│ ├── index.js
│ └── package.json
└── yarn.lockpkg-2依赖于pkg-1.
pkg-1/index.js如下所示
export function printName(){
console.log('my name');
}
console.log('print name')pkg-2/index.js如下所示
import { printName } from "pkg-1";
printName()// babel.config.js
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};pkg-2/package.json
{
"name": "pkg-2",
"packageManager": "yarn@3.2.0",
"scripts": {
"start": "babel-node index.js"
},
"dependencies": {
"pkg-1": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.17.10",
"@babel/node": "^7.17.10",
"@babel/preset-env": "^7.17.10"
}
}运行命令yarn workspace pkg-1 run start将按预期打印print name。但是,运行yarn workspace pkg-2 run start会导致以下结果
/Users/mds31/Documents/testing/yarn/packages/pkg-1/index.js:1
export function printName(){
^^^^^^
SyntaxError: Unexpected token 'export'
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Module._compile (/Users/mds31/Documents/testing/yarn/.yarn/cache/pirates-npm-4.0.5-22f8e827ce-c9994e61b8.zip/node_modules/pirates/lib/index.js:136:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at require$$0.Module._extensions..js (/Users/mds31/Documents/testing/yarn/.pnp.cjs:13055:33)
at Object.newLoader [as .js] (/Users/mds31/Documents/testing/yarn/.yarn/cache/pirates-npm-4.0.5-22f8e827ce-c9994e61b8.zip/node_modules/pirates/lib/index.js:141:7)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.require$$0.Module._load (/Users/mds31/Documents/testing/yarn/.pnp.cjs:12895:14)
at Module.require (node:internal/modules/cjs/loader:1005:19)这大概是因为pkg-1没有被编译,是否有我缺少的配置设置?
发布于 2022-05-13 12:30:28
找到了解决此问题的方法,它涉及在使用yarn node运行之前构建所有包。使用这个工具https://yarn.build/运行所有工作区的build命令,这会使这一点变得更容易一些。
现在运行的脚本如下所示
{
"main": "dist/index.js",
"scripts": {
"build": "babel index.js --out-dir dist",
"start": "yarn build && yarn node dist/index.js"
}
} https://stackoverflow.com/questions/72214942
复制相似问题