Rename package.json Package Name When Publishing in Monorepo - npm

I'm looking to rename a package in a yarn workspaces monorepo when publishing that package.
The project structure looks something like:
package.json (a)
core
log
package.json (b)
services
...
The root package.json (a) uses the org #companyA in its name and the log package.json (b) has a name of #companyA/log. This can be imported into other services within the monorepo by importing it like:
import { log } from '#companyA/log';
and that works great. The problem is that #companyA isn't what we would like to publish this package to. Instead we would like to use #companyB. Is there a way to easily change the package.json name when publishing? Can Lerna do something like this?

Related

NPM Workspaces with two or more #prisma/client dependencies

I have a monorepo setup. It looks something like this:
project
node_modules
packages
my-first-project
prisma
schema.prisma
my-second-project
prisma
schema.prisma
So both projects (my-first-project and my-second-project) have #prisma/client installed and get there dependencies from the upper node_modules folder.
The thing is that whenever i change something in my schema.prisma file (e.g. in my-first-project) and run npx prisma migrate dev --name whatever it generates all the types and stuff and puts it in the upper node_modules folder. this leads to "type not found" errors on the other project (e.g. my-second-project).
Is there a way to tell npm to keep some dependencies in a separate node_modules folder inside each project?
You could configure a custom output path to specify the location at which PrismaClient should be generated.
Example:
generator client {
provider = "prisma-client-js"
output = "../src/generated/client"
}
And you should be able to import PrismaClient like this:
import { PrismaClient } from './generated/client'
By generating PrismaClient outside of node_modules should solve your issue.

Can I publish my project to npm, not as a package but a whole project including folders and files?

Can I publish my project to npm, not as a package but a whole project (folder and file)? So I can init it again using command like "npm init my-project".
Maybe it is like a git clone but my boss want it using npm. Can I do that?
So all the structure is included when I init it, like folder and files.

How to access the dependency versions in a Create-React-App

In a create-react-app i would like to access some properties of the package.json and show those to the user in the browser. Like the version of the app and the version of some of the dependencies specified in the package.json.
How would I access those properties, without importing and exposing the whole package.json to the client?
Executing npm run build on the create-react-app provides a production bundle in the ./build directory.
Solution 1:
The way it works it does not expose the rest of the package.json content to the production bundle when making a destructured import. (E.g. previous answer from Devchris)
import { dependencies } from './package.json';
Solution 2:
By extending the npm scripts it is possible to read and expose the package.json into the node environment and read it from there at build time (https://create-react-app.dev/docs/adding-custom-environment-variables)
process.env.REACT_APP_DEPENDENCIES
Note: The variable must start with 'REACT_APP_'
What you can do is:
import { version, dependencies } from './package.json';
this will give you all dependencies and the version of your package.json in your js code. Keep in mind that your path to the package.json file might be different.

How to deploy a package for a private gitlab dependency in Yarn

I am working on a vue project that needs to use another private vue project as a dependency. This other private project is a vue plugin.
I have found how to tell yarn to fetch a package on a private gitlab repository by adding the following line in package.json:
"dependencies": {
"myPackage": "git+https://{token-name}:{token}#gitlab.com/path/to/repo.git#someTag"
}
This works well, and the content of my repo is downloaded in my node_modules. However, here comes my problem :
In this repo, the actual vue plugin is not at the root, it's under a subfolder of the repo, meaning the index.js at the root of the repo is not the one from my plugin (and I guess it is the one yarn will be using).
I have a custom yarn deploy script that compiles my plugin into one JS file and put it in a dist folder, however the dist folder is not versioned. I can use the gitlab CI to generate it, but still i'm pretty sure yarn won't use what is inside the dist folder.
My (broad) question is : how can I use the tools at my disposition (yarn, gitlab-ci) to be able to use my private gitlab repository as a vue-plugin for one of my other project ?
You would tell other packages how to use your packages by using the properties of your package.json. For instance, the main declaration
{
main: 'dist/index.js'
}
This tells node how to resolve your module from your package.
so require('my-vue-plugin') or import MyVuePlugin from 'my-vue-plugin' would resolve to node_modules/my-vue-plugin/dist/index.js, for example.
As far as versioning is concerned -- you don't version the file, or the folder. You version through the version property of your package.json and, in your case, through GIT by using git tag -a v(major).(minor).(patch).
The version that you tag should match the version that you specify in package.json.
I would recommend reading more about semantic versioning and creating a script (like VueJS) to auto-increment your package, version and publish.

How does node.js require('xxx') knows which javascript file is the main entrance in xxx package?

I'm trying to write a node module myself and publish it.
Assume the dir looks like below:
root/
a.js
b.js
c.js
package.json
now a.js and b.js are required in c.js, and I want c.js to be the package interface when other developers require my package. How to specify that? I don't have a clue.
When my package is required, how does npm knows which js file is the main entrance?
It can be specified as main field in package.json: "main": "c.js"
https://docs.npmjs.com/files/package.json#main
By default index.js from package folder would be used.
https://nodejs.org/api/modules.html#modules_folders_as_modules