Bruno Quaresma
Bruno Quaresma

Reputation: 10707

NPM Package Development - How can i execute the build command after package install?

how do i to generate the build folder after my package be installed?

i did this:

"scripts": {
    "build": "babel ./src --out-dir ./build"
  }

But when other user install the package the npm not build.

How can i execute the build command after install?

Upvotes: 1

Views: 183

Answers (2)

McMath
McMath

Reputation: 7188

It's almost certainly best not to have a post-install step at all. What you really want to do is to build the project before it is published, and then publish the built version to NPM (assuming that's what you are trying to do). In that case, you might use a prepublish script:

// package.json

"scripts": {
  "build": "babel src -d build",
  "prepublish": "npm run build"
}

And make sure the built files are included in your files array:

// package.json

"files": ["build"]

Or that your source files are excluded in your .npmignore:

// .npmignore

/src/

If you really do need a post-install step for some reason, use a postinstall script:

// package.json

"scripts": {
  "build": "babel src -d build",
  "postinstall": "npm run build"
}

But I can't think of a use-case where this would be a good idea.

Upvotes: 1

Mr. Hedgehog
Mr. Hedgehog

Reputation: 2885

You should use "postinstall" in scripts.

source: https://docs.npmjs.com/misc/scripts

Upvotes: 1

Related Questions