简体   繁体   English

在节点上运行npm-scripts

[英]Running npm-scripts on node

I am trying to get away from using Grunt or Gulp in my projects. 我试图摆脱在项目中使用Grunt或Gulp的麻烦。 I thought that a good way to replace them is by using npm-scripts. 我认为替换它们的一个好方法是使用npm-scripts。

I know that npm-scripts leverages package.json , but I noticed that to run more advanced build processes, you need to include command line functions. 我知道npm-scripts利用package.json ,但是我注意到要运行更高级的构建过程,您需要包括命令行功能。 However, this is not a cross-platform solution since Windows doesn't support the wide variety of commands that an OS like Linux supports. 但是,这不是跨平台解决方案,因为Windows不支持Linux之类的OS支持的各种命令。

So, I was wondering if you could just run npm-scripts on Node, and just reference any npm package you want to with a require statement. 因此,我想知道您是否可以仅在Node上运行npm-scripts,并仅通过require语句引用您想要的任何npm软件包。

Is this possible? 这可能吗? If not, are there any good cross-platform solutions that exist for npm-scripts excluding Grunt and Gulp? 如果没有,那么除了Grunt和Gulp之外,对于npm-scripts是否存在任何良好的跨平台解决方案?

Yes you could run "npm-scripts on node". 是的,您可以在节点上运行“ npm-scripts”。 For instance I have this in my package.json (irrelevant parts are removed for clarity), and both rimraf and webpack are implemented in pure JS and interpreted by node.js. 例如,我在package.json有此内容(为了清楚起见,删除了不相关的部分),并且rimrafwebpack均在纯JS中实现,并由node.js解释。 In fact rimraf is a good example of cross platform rm -Rf . 实际上, rimraf是跨平台rm -Rf一个很好的示例。 This solution runs fine on windows, mac or linux boxes by just issuing npm run-script build . 只需发出npm run-script build ,此解决方案就可以在Windows,Mac或Linux机器上npm run-script build

{
    "scripts": {
       "build": "rimraf dist && webpack --config ./blah.js"
    },
    "devDependencies": {
        "rimraf": "^2.5.0",
        "webpack": "^1.12.10"
    }
}

Or you could do something like: 或者,您可以执行以下操作:

"scripts": {
    "hello": "node hello"
}

And implement everything you want in hello.js in the same dir as your package.json , and include whatever you need in that script like: 并在hello.js中与package.json相同的目录中实现所需的所有内容,并在该脚本中包含所需的内容,例如:

const hello = require("debug")("hello"); // require whatever module you need
console.log("hello world");

It would run just fine with npm run-script hello 使用npm run-script hello可以正常npm run-script hello

> your-module@1.0.0 hello D:\dev\tmp
> node hello

hello world

I would stick with webpack and npm only. 我只会坚持使用webpack和npm。 Using webpack is much simpler than writing custom grunt or gulp tasks, dev server will give a stable local enviornment to work, and it is just a config setup and you are golden. 使用webpack比编写自定义的grunt或gulp任务简单得多,开发服务器将为工作提供稳定的本地环境,并且它只是配置设置,您真是天花乱坠。 I would also move away from using global installs especially if you work team environment or are running continuous delivery. 我也将不再使用全局安装,尤其是在您工作团队环境或正在连续交付的情况下。 As you won't be able to control the global installation or installed version on any of these environments. 因为您将无法在任何这些环境中控制全局安装或已安装的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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