Npm POSTSS cli:使用本地和全局安装的POSTSS模块

Npm POSTSS cli:使用本地和全局安装的POSTSS模块,npm,postcss,Npm,Postcss,如何在本地安装postsss模块,并通过全球安装的postsss cli使用它们 例如: postcss -u autoprefixer -o style.css index.css 如果全局安装了postsss cli和autoprefixer,或者两者都在本地安装,则此命令有效。但是如果我在全局安装了postsss cli,并且在本地安装了autoprefixer,那么它就不起作用了 这个问题并不特定于autoprefixer,因为autoprefixer将是一个模块,我想知道全球安装了什

如何在本地安装
postsss
模块,并通过全球安装的
postsss cli
使用它们

例如:

postcss -u autoprefixer -o style.css index.css
如果全局安装了
postsss cli
autoprefixer
,或者两者都在本地安装,则此命令有效。但是如果我在全局安装了
postsss cli
,并且在本地安装了
autoprefixer
,那么它就不起作用了

这个问题并不特定于autoprefixer,因为autoprefixer将是一个模块,我想知道全球安装了什么。任何建议都会有帮助

如果全局安装了
postss cli
,而本地安装了
autoprefixer
,则会出现以下错误:

> postcss -u autoprefixer -o style.css index.css -w

module.js:327
    throw err;
    ^

Error: Cannot find module 'autoprefixer'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at /home/user/.nvm/versions/node/v4.4.2/lib/node_modules/postcss-cli/index.js:104:14
    at Array.map (native)
    at Object.<anonymous> (/home/user/.nvm/versions/node/v4.4.2/lib/node_modules/postcss-cli/index.js:97:24)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)

npm ERR! Linux 3.19.0-58-generic
npm ERR! argv "/home/user/.nvm/versions/node/v4.4.2/bin/node" "/home/user/.nvm/versions/node/v4.4.2/bin/npm" "run" "css"
npm ERR! node v4.4.2
npm ERR! npm  v2.15.0
npm ERR! code ELIFECYCLE
npm ERR! Node.js@1.0.0 css: `postcss -u autoprefixer -o style.css index.css -w`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the Node.js@1.0.0 css script 'postcss -u autoprefixer -o style.css index.css -w'.
npm ERR! This is most likely a problem with the Node.js package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     postcss -u autoprefixer -o style.css index.css -w
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs Node.js
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! 
npm ERR!     npm owner ls Node.js
npm ERR! There is likely additional logging output above.
>postsss-u autoprefixer-o style.css index.css-w
module.js:327
犯错误;
^
错误:找不到模块“autoprefixer”
在Function.Module.\u解析文件名(Module.js:325:15)
在Function.Module.\u加载(Module.js:276:25)
at Module.require(Module.js:353:17)
根据需要(内部/module.js:12:17)
at/home/user/.nvm/versions/node/v4.4.2/lib/node_modules/postss cli/index.js:104:14
at Array.map(本机)
反对。(/home/user/.nvm/versions/node/v4.4.2/lib/node_modules/postss cli/index.js:97:24)
在模块处编译(Module.js:409:26)
在Object.Module.\u extensions..js(Module.js:416:10)
在Module.load(Module.js:343:32)
npm错误!Linux 3.19.0-58-generic
npm错误!argv“/home/user/.nvm/versions/node/v4.4.2/bin/node”“/home/user/.nvm/versions/node/v4.4.2/bin/npm”“运行”“css”
npm错误!节点v4.4.2
npm错误!npm v2.15.0
npm错误!代码失效循环
npm错误!节点。js@1.0.0css:`postsss-u autoprefixer-o style.css index.css-w`
npm错误!退出状态1
npm错误!
npm错误!在节点上失败。js@1.0.0css脚本'postsss-u autoprefixer-o style.css index.css-w'。
npm错误!这很可能是Node.js包的问题,
npm错误!与npm本身无关。
npm错误!告诉作者此操作在您的系统上失败:
npm错误!postsss-u autoprefixer-o style.css index.css-w
npm错误!您可以通过以下方式获取有关如何打开此项目问题的信息:
npm错误!npm bugs Node.js
npm错误!或者,如果不可用,您可以通过以下方式获取他们的信息:
npm错误!
npm错误!npm所有者ls Node.js
npm错误!上面可能还有其他日志输出。

如果使用postcss cli,可以创建如下配置文件:

{
    "use": [
      "postcss-import",
      "postcss-cssnext",
      "lost",
      "rucksack-css",
      "postcss-autoreset",
      "postcss-font-magician"
    ],
    "input": "src/app.css",
    "output": "css/main.css",
    "local-plugins": true,
    "autoprefixer": {
      "browsers": "last 2 versions"
    }
}
请注意
“本地插件”:true,
行。从文档中:

从当前工作目录中的节点_模块开始查找插件。如果没有此选项,POSTSS cli将在安装它的节点_模块中查找插件-特别是如果它是全局安装的,它将只查找全局安装的插件

如果您不想使用配置文件,可以使用选项
--localplugins

总是在本地安装是个好主意。您始终可以在
package.json
中创建NPM脚本,以便更轻松地运行
postsss cli
,如下所示:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "node_modules/postcss-cli/bin/postcss -c config.json",
    "watch": "node_modules/postcss-cli/bin/postcss -c config.json -w",
    "minify": "node_modules/postcss-cli/bin/postcss -c config-minify.json"
 },
现在您只需使用:

npm run build

我会在本地安装所有东西。这会困扰你吗?@topheman在一个地方为每个项目安装一次cli是很方便的。如果我遇到同样的错误,我会在本地安装所有东西,定义一些npm脚本,如
/node\u modules/.bin/postsss-u autoprefixer-o style.css index.css
(一件好事是,协作者在登陆项目时不需要任何全局依赖关系)-但我也能理解您希望只安装一次cli。这似乎是一个bug,您应该提交到
postsss cli
存储库中。@topheman谢谢,知道它可能是bug是很有帮助的。更容易的协作也是一个很好的观点。我刚刚偶然发现了您的答案,我想知道您对做了什么ode>minify命令。你能解释一下你在使用
-c config minify.json
部分做什么吗?我正在使用Tailwind,我的生产脚本如下:
“prod”:“NODE\u ENV=production postsss styles.css-o assets/Tailwind.css”
非常感谢!