Npm Install from Repo Not Running `Prepare`

I have an npm package for common components hosted on an internal git server. For some reason when I call npm install in another project I want to consume this in it will not run the prepare hook. Obviously, this does not work since the npm package needs a /dist folder in node_modules to be able to consume the package.

I have already tried things such as using the deprecated prepublish hook and even that does not get called. I also tried to do postinstall to see if I could build after install, while that hook did get called it failed because the devDependencies were not installed

package.json

{
  "name": "common-components",
  "version": "0.1.0",
  "scripts": {
    "prepare": "npm run build",
    "build": "ng build",
    ...
  },
  "private": true,
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  },
}

command being used for install

npm install --save git+ssh://{URL-to-common-components-repo}}

I have read through the npm-scripts documentation thoroughly and it seems like they insist that prepare hook should always be called for this exact use-case

Updated 5/6/2019

Just as a note I found this bug on NPM community .

I am using npm 6.4.1 which should work according to the bug

0

4 Answers

One thing to check that hit me on a package recently - if there is a .gitignore and not .npmignore npm may be ignoring your /dist folder. Adding an empty .npmignore worked in this case.

"If there’s no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file. If you want to include something that is excluded by your .gitignore file, you can create an empty .npmignore file to override it."

from

1

For those that are wondering the status of this. I was unable to ever get it to work. What I ended up doing was hosting the components on a private npm registry and that works fine since the npm publish command will do the build and only publish the dist folder

2

If adding an empty .npmignore does not help, you can try specifying all files in dist explicitly in package.json#files. If this works you might want to consider using a wildcard pattern that matches the files in dist to simplify the package.json.

package.json

    ...
    "files": [
        "source",
        "dist/cjs/main.js",
        "dist/es/main.js"
    ]
}

see this comment to a similar issue in the npm/cli repository

Adding main in the package.json fixed this issue for me.

    "main": "./dist/index.js",
    "scripts": {
       "build": "babel src --out-dir dist",
       "prepare": "npm run build",
       "lint": "eslint ."
     },

node v14.15.4 npm 6.14.11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest