Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
TS Read Dependency version from the script inside package.json

I have a react project with a package.json that has below structure:

"name": "@repo/packagex",
  "description": "package X",
  "version": "1.0.0",
  "scripts": {
    "build": "some script to move resources into a folder named resources/3.4.5/",

  },
  "dependencies": {
    "@repo/package-y": "3.4.5",

}
...

If you notice, I am trying to copy some resources into a folder named as the version number of my @repo/package-y, and therefore whenever someone updates the version of @repo/package-y, the copy folder name should change to that version.

Solutions tried: i tried doing as below but doesn't resolve the version number when deployed but sol 1 works fine locally:

sol 1 tried
resources/${npm_package_dependencies__repo_package_y}

sol 2 tried
resources/%npm_package_dependencies__repo_package_y%

Is there a cross-platform way to access this version?. If I was working in a js/ts function i'd simply import the package.json file object and get whatever properties I want but now this is inside the package.json file.

PS: Using webpack and babel

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As you mentioned Webpack, I think you could use copy-webpack-plugin

assuming in your case the package.json would look following:

{
  "name": "@repo/packagex",
  "version": "1.0.0",
  "scripts": {
    "build": "webpack"
  },
  "dependencies": {
    "@repo/package-y": "3.4.5"
  },
  "devDependencies": {
    "copy-webpack-plugin": "^10.2.4",
    "webpack": "^5.70.0",
    "webpack-cli": "^4.9.2"
  }
}

  • './src/index.js' default entry for webpack should exists to not allow webpack to complain with the config below
  • './some-resources/file.xyz' will be copied if exists

webpack.config.js content:

const {dependencies} = require('./package.json');
const CopyWebpackPlugin = require('copy-webpack-plugin');

/**
 * @type {import('webpack').Configuration}
 */
const config = {
  mode: 'development',
  plugins: [
    new CopyWebpackPlugin({
      patterns: Object.keys(dependencies).map((dep) => ({
        to: `resources/${dependencies[dep]}/`,
        from: '*.*',
        context: './some-resources'
      }))
    })
  ]
};

module.exports = config;

the output will be placed under the following path ./dist/resources/3.4.5/some-resources/file.xyz

If all goes well in console webpack should throw smth like the following output

@repo/packagex@1.0.0 build
webpack

asset main.js 1.18 KiB [compared for emit] (name: main)
asset resources/3.4.5/file.xyz 0 bytes [compared for emit] [from: some-resources/file.xyz] [copied]<br/>
./src/index.js 1 bytes [built] [code generated]
webpack 5.70.0 compiled successfully in 49 ms
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!