NPM包开发 - 如何在安装包后执行build命令?

时间:2022-08-15 22:57:42

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

如何在安装软件包后生成build文件夹?

i did this:

我这样做了:

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

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

但是当其他用户安装包时,npm没有构建。

How can i execute the build command after install?

如何在安装后执行build命令?

2 个解决方案

#1


1  

You should use "postinstall" in scripts.

您应该在脚本中使用“postinstall”。

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

#2


1  

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:

几乎可以肯定最好不要安装后安装步骤。您真正想要做的是在项目发布之前构建项目,然后将构建的版本发布到NPM(假设您正在尝试这样做)。在这种情况下,您可以使用预发布脚本:

// 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中:

// .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.

但我想不出一个用例,这会是一个好主意。

#1


1  

You should use "postinstall" in scripts.

您应该在脚本中使用“postinstall”。

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

#2


1  

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:

几乎可以肯定最好不要安装后安装步骤。您真正想要做的是在项目发布之前构建项目,然后将构建的版本发布到NPM(假设您正在尝试这样做)。在这种情况下,您可以使用预发布脚本:

// 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中:

// .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.

但我想不出一个用例,这会是一个好主意。