Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda