serverless-esbuild
serverless-esbuild copied to clipboard
Trying to find package.json in the wrong path with pnpm
Description
I changed the package from "lerna" to "pnpm" last week. And I changed "serverless-plugin-monorepo" to this plugin "serverless-esbuild"
Can you tell me why plugin trying to find package.json in the wrong path It's hard for me to figure it out.
How can I debug plugin with this logs ? I don't know much, so please understand even if it's an inappropriate question 🙏
Project structure
Projects // <-- esbuild find package.json in this path
┗ MY_PROJECT
┗ package.json
┗ ...
┗ packages
┗ MY_PDF
┗ package.json // <-- instead of this
┗ ...
┗ ...
Error logs
Projects/MY_PROJECT/packages/MY_PDF $ pnpm serverless package
Packaging MY_PDF for stage dev (ap-northeast-2)
Compiling to node14 bundle with esbuild...
Compiling with concurrency: Infinity
Compiling completed.
Environment: darwin, node 14.17.0, framework 3.2.1 (local), plugin 6.1.0, SDK 4.3.1
Docs: docs.serverless.com
Support: forum.serverless.com
Bugs: github.com/serverless/serverless/issues
Error:
Error: ENOENT: no such file or directory, open '/Users/donald/Projects/package.json'
at Object.openSync (fs.js:498:3)
at Object.readFileSync (fs.js:394:35)
at readFileSync (/Users/donald/Projects/MY_PROJECT/node_modules/.pnpm/[email protected]/node_modules/serverless/lib/utils/fs/read-file-sync.js:7:24)
at Utils.readFileSync (/Users/donald/Projects/MY_PROJECT/node_modules/.pnpm/[email protected]/node_modules/serverless/lib/classes/utils.js:78:12)
at EsbuildServerlessPlugin.packExternalModules (/Users/donald/Projects/MY_PROJECT/node_modules/.pnpm/[email protected][email protected]/node_modules/serverless-esbuild/dist/pack-externals.js:180:51)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async before:package:createDeploymentArtifacts (/Users/donald/Projects/MY_PROJECT/node_modules/.pnpm/[email protected][email protected]/node_modules/serverless-esbuild/dist/index.js:81:17)
at async PluginManager.runHooks (/Users/donald/Projects/MY_PROJECT/node_modules/.pnpm/[email protected]/node_modules/serverless/lib/classes/plugin-manager.js:522:35)
at async PluginManager.invoke (/Users/donald/Projects/MY_PROJECT/node_modules/.pnpm/[email protected]/node_modules/serverless/lib/classes/plugin-manager.js:551:9)
at async PluginManager.run (/Users/donald/Projects/MY_PROJECT/node_modules/.pnpm/[email protected]/node_modules/serverless/lib/classes/plugin-manager.js:592:7)
at async Serverless.run (/Users/donald/Projects/MY_PROJECT/node_modules/.pnpm/[email protected]/node_modules/serverless/lib/serverless.js:169:5)
at async /Users/donald/Projects/MY_PROJECT/node_modules/.pnpm/[email protected]/node_modules/serverless/scripts/serverless.js:686:9
pnpm support is a little sketchy with this plugin. We don't support invoking from a folder which does not contain package.json in the root. eg. in your case when you invoke sls with pnpm it invokes it from MY_PROJECT by the looks. I'm not too sure how to help you as I'm not across pnpm.
Actually you might be able to use packagePath
I found it.
Summary
The cause was the yarn.lock file in that path ( /Users/donald/Projects/yarn.lock ) remove yarn.lock file, problem solved!
For someone like me, let's be a little more detailed. Let me know if what I've figured out is wrong.
Detail
As shown in the error log above
at EsbuildServerlessPlugin.packExternalModules (/Users/donald/Projects/MY_PROJECT/node_modules/.pnpm/[email protected][email protected]/node_modules/serverless-esbuild/dist/pack-externals.js:180:51)
pack-externals.js:180:51
const rootPackageJson = this.serverless.utils.readFileSync(rootPackageJsonPath);
and rootPackageJsonPath variable declare and initialized in same file line 163
const rootPackageJsonPath = path_1.default.join((0, utils_1.findProjectRoot)() || '', './package.json');
and utils_1.findProjectRoot is ...
function findProjectRoot(rootDir) {
return rootDir !== null && rootDir !== void 0 ? rootDir : findUp(['yarn.lock', 'package-lock.json']);
}
"findUp" function is called recursively until ['yarn.lock', 'package-lock.json'] met My project package manager is pnpm, So there is no [yarn.lock, package-lock.json]. Except for that Projects directory's yarn.lock file
I can't remember exactly why that file exists there. 😢
Thanks for your feedback @samchungy.
Also encountered this issue with pnpm
. Luckily I was able to temporary fix it by adding empty pacakge-lock.json
in the folder.
I encountered this as well using plain ol' npm
— for me I had accidentally ran yarn install
in the parent dir of my Serverless project some days prior to attempting a deploy. Lead me down quite a rabbit hole before stumbling on this issue!