【问题标题】:postcss-cli: using locally vs globally installed postcss modulespostcss-cli:使用本地和全局安装的 postcss 模块
【发布时间】:2016-08-04 08:16:08
【问题描述】:

如何在本地安装postcss模块,并通过全局安装的postcss-cli使用它们?

例如:

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

如果我全局安装了postcss-cliautoprefixer,或者两者都在本地安装,则此命令有效。但它不起作用,如果我在全局安装了postcss-cli,在本地安装了autoprefixer

这个问题并不特定于 autoprefixer,因为 autoprefixer 将是一个模块,无论如何我都会在全球范围内安装什么。任何建议都会有所帮助。

这是我得到的错误,如果postcss-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.

【问题讨论】:

  • 我会在本地安装所有东西。打扰你了吗?
  • @topheman 每个项目在一个地方安装一次cli很方便。
  • 如果我遇到同样的错误,我会在本地安装所有东西,定义一些像 ./node_modules/.bin/postcss -u autoprefixer -o style.css index.css 这样的 npm 脚本(好在合作者不需要 任何 登陆项目时的全局依赖) - 但我也可以理解您希望只安装一次 cli。这似乎是一个应该提交到postcss-cli 存储库的错误。
  • @topheman 谢谢,知道它可能是错误会很有帮助。更轻松的协作也是一个好处。

标签: npm postcss


【解决方案1】:

如果你使用 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"
    }
}

注意"local-plugins": true, 行。来自文档:

从当前工作目录中的 node_modules 开始查找插件。如果没有这个选项,postcss-cli 将在它安装的 node_modules 中查找插件 - 特别是如果它是全局安装的,它只会查找全局安装的插件。

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

始终在本地安装是个好主意。您始终可以在 package.json 中创建 NPM 脚本,以便更轻松地运行 postcss-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

【讨论】:

  • 我只是偶然发现了你的答案,我想知道你在用minify 命令做什么。你能解释一下你在用-c config-minify.json 部分做什么吗?我正在使用 Tailwind,我的生产脚本如下所示:"prod": "NODE_ENV=production postcss styles.css -o assets/tailwind.css" 非常感谢!
猜你喜欢
  • 2018-03-17
  • 2016-03-13
  • 2017-07-29
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 2015-02-07
  • 2021-06-17
  • 1970-01-01
相关资源
最近更新 更多