在 NPM 脚本中找不到模块自动前缀

Bar*_*eby 5 npm autoprefixer postcss npm-scripts

在 Windows 10 和 npm 版本 6.9.0 下,我无法让以下脚本运行:

"build:css": "postcss --use autoprefixer -b 'last 2 versions' < ./static/css/main.css"  
Run Code Online (Sandbox Code Playgroud)

我总是在 Windows 控制台中收到相同的错误,如下所示:

Plugin Error: Cannot find module 'autoprefixer'

我尝试将语法更改为以下语法,但没有结果:

"build:css": "postcss --use autoprefixer -b \"last 10 versions\" ./static/css/main.css -o ./static/css/main-prefixed.css"
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这里有什么问题吗?老实说,我对这个 npm 脚本很陌生。

我的package.json看起来像这样:

{
  "name": "test-automate-npm",
  "version": "1.0.0",
  "description": "test",
  "main": "index.html",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "scss": "echo 'Compiling SCSS' && node-sass --watch scss -o ./static/css",
    "build": "npm run scss && npm run build:css",
    "build:css": "postcss --use autoprefixer -b 'last 2 versions' < ./static/css/main.css"   
  },
  "keywords": [
    "keywords",
    "here"
  ],
  "author": "Barleby",
  "license": "ISC",
  "dependencies": {
    "node-sass": "^4.12.0"
  },
  "devDependencies": {
    "autoprefixer": "^6.3.1",
    "postcss": "^5.0.16",
    "postcss-cli": "^2.5.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

先感谢您!


编辑:

我还尝试定义package.jsonbuild:css中命名的 npm 脚本,如下所示:

"build:css": "postcss ./static/css/main.css autoprefixer -b \"last 10 versions\" -o ./static/css/main-prefixed.css"
Run Code Online (Sandbox Code Playgroud)

这会导致以下错误打印到控制台:

您没有设置任何插件、解析器或字符串生成器。现在,PostCSS 什么也不做。在postcss.parts上为您的案例选择插件,并在 postcss.config.js 中使用它们。

Rob*_*obC 3

  1. 创建一个名为postcss.config.js包含以下内容的新文件:

    module.exports = {
      plugins: {
        autoprefixer: {
          browsers: ['last 10 versions']
        }
      }
    };
    
    Run Code Online (Sandbox Code Playgroud)

    将新创建的 postcss.config.js文件保存在项目目录的根目录中,即将其保存在所在目录中package.json

  2. build:css将您中命名的 npm 脚本更改package.json为以下内容:

    ...
    "scripts": {
      ...
      "build:css": "postcss ./static/css/main.css -o ./static/css/main-prefixed.css"
    },
    ...
    
    Run Code Online (Sandbox Code Playgroud)