简体   繁体   中英

NPM build phase of my package not executed during install

I am developing a package to use web workers in a generic way but I am finding some issues when trying to add it as a dependency for another project.

Normally I would expect that having a build script section of my package.json when doing install that it would be automatically called generating the output of the rollup.config.js . But it does not seem to execute anything. Do I have any misunderstanding on how npm build should be working?

If not, there be any other colliding script in package.json that is causing it not to work in the next file example:

{
 "name": "web-threads",
  "version": "1.0.5",
  "description": "generic threads using web workers for the web",
  "main": "dist/web-threads.js",
  "scripts": {
    "build": "rollup -c",
    "test": "jest",
    "test:dev": "jest --watchAll test/unit",
    "test:int": "jest test/integration",
    "test:cov": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
    "push": "yarn test && git push",
    "deploy:major": "yarn version --major",
    "deploy:minor": "yarn version --minor",
    "deploy:patch": "yarn version --patch",
    "deploy:push": "git push && git push --tags",
    "preversion": "yarn test"
  },
  "keywords": [""],
  "repository": "",
  "author": "",
  "license": "MIT",
  "private": false,
  "devDependencies": {
    "babel-jest": "23.4.2",
    "babel-preset-env": "1.7.0",
    "babel-preset-stage-0": "6.24.1",
    "coveralls": "3.0.2",
    "faker": "4.1.0",
    "jest": "23.5.0",
    "jest-puppeteer": "3.3.1",
    "puppeteer": "1.7.0",
    "rollup": "0.65.0",
    "rollup-plugin-babel": "3.0.7",
    "rollup-plugin-uglify": "4.0.0",
    "uglify-es": "3.3.9"
  },
  "babel": {
    "presets": ["env","stage-0"]
  },
  "jest": {
    "testMatch": [
      "**/test/**/*-test.js"
    ],
    "transform": {
      "^.+\\.jsx|.js?$": "babel-jest"
    }
  }
}

I also moved the dependencies to not be devDependencies but it didn't help solving the issue.

NPM build documentation: https://docs.npmjs.com/cli/build

You could try adding a postinstall script. As documented in the npm docs

postinstall: Run AFTER the package is installed.

So the answer of @Olian04 send me in the right direction and dig a bit on the documentation. Indeed I had a misunderstanding regarding build as it is actually not a script but just a hook to the process stage.

So seems the correct way of solving the compilation required in packages is a different process run by prepare . This is a script that documentation defines as:

For build steps that are not platform-specific, such as compiling CoffeeScript or other languages to JavaScript, use the prepare script to do this, and make the required package a devDependency.

The prepare script will be run before publishing so that users can consume the functionality without requiring them to compile it themselves. In dev mode (ie, locally running npm install), it'll run this script as well, so that you can test it easily.

Example:

{ "name": "web-threads",
  "description": "a delightfully fruity coffee varietal",
  "version": "1.2.3",
  "devDependencies": {
    "coffee-script": "~1.6.3"
  },
  "scripts": {
    "prepare": "coffee -o lib/ -c src/waza.coffee"
  },
  "main": "lib/waza.js"
}

As a summary use postinstall for things that need to happen locally to the installing computer/platform (but it will require all dependencies to be satisfied). Use prepare for process that are not platform dependant this will not require the user to have all the tools to trnspile the package and you will also not polute your repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM