使用“npm run build"失败并显示“npm ERR!缺少脚本:构建" [英] Using "npm run build" fails with "npm ERR! missing script: build"

查看:72
本文介绍了使用“npm run build"失败并显示“npm ERR!缺少脚本:构建"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何解决这个错误,我运行的是 Windows 10

当我尝试在 cmd 上 npm run build 时出现此错误

C:Usersanai_>npm 运行构建npm 错误!缺少脚本:构建

这里是运行日志

0 info 如果它以 ok 结尾,它就起作用了1 详细的 cli ['C:\Program Files\nodejs\node.exe',1 详细的 cli 'C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js',1个详细的cli运行",1 个详细的 cli 'build']2 信息使用 npm@5.5.13 信息使用 node@v9.1.04 详细配置跳过项目配置:C:Usersanai_/.npmrc.(匹配用户配置)5 详细堆栈错误:缺少脚本:构建5 运行时的详细堆栈 (C:Program Files
odejs
ode_modules
pmlib
un-script.js:151:19)5 详细堆栈在 C:Program Files
odejs
ode_modules
pmlib
un-script.js:61:55 详细堆栈在 C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:115:55 详细堆栈在 C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:402:5checkBinReferences_ (C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:357:45) 中的 5 详细堆栈5 详细堆栈在最终 (C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:400:3)5 详细堆栈 (C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:160:5)5 ReadFileContext 中的详细堆栈.<anonymous>(C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:332:20)5 ReadFileContext.callback 中的详细堆栈 (C:Program Files
odejs
ode_modules
pm
ode_modulesgraceful-fsgraceful-fs.js:78:16)5 FSReqWrap.readFileAfterOpen [as oncomplete] 的详细堆栈 (fs.js:437:13)6 详细 cwd C:Usersanai_7 详细的 Windows_NT 10.0.150638 详细 argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "建造"9 详细节点 v9.1.010 冗长的 npm v5.5.111 错误缺少脚本:构建12 详细退出 [ 1, true ]

虽然错误是指缺少脚本构建,但我已经在我的 package.json 中放置了一个构建"脚本

<代码>{"name": "es6",版本":1.0.0","main": "index.js",脚本":{构建":webpack"},关键字":[],作者": "","许可证": "ISC",开发依赖":{"webpack": "^3.8.1"}}

这是我的 webpack.config.js 文件

module.exports = {条目:['./app/index.js'],输出: {路径:'./build',文件名:'bundle.js'}}

谁能告诉我如何修复这个错误或者它来自哪里.我是使用 node.js、webpack 和命令行的新手.提前感谢您的时间!

解决方案

  1. 尝试将 webpack.config.js 中的 output.path 设置为绝对路径

    module.exports = {条目:['./app/index.js'],输出: {路径:__dirname + '/build',文件名:'bundle.js'}}

    我没有 Windows(在 Linux 上)但使用节点 8.16.0 和 npm 6.4.1,复制您的 package.json 和 webpack.config.js,我收到以下错误:

    无效的配置对象.Webpack 已使用与 API 架构不匹配的配置对象进行初始化.- configuration.output.path:提供的值./build"不是绝对路径!->输出目录为**绝对路径**(必需).

我为模仿您的设置所做的步骤:

  1. 复制你的 package.json 和 webpack.config.js
  2. 运行 npm i(或 npm install)
  3. run npm run build - 收到上面提到的无效配置错误
  4. 将 output.path 更改为与 __dirname 连接的绝对路径(不确定 Windows 是否不同...)
  5. 重新运行 npm run build 并得到:

    多个 ./app/index.js 中的错误找不到模块:错误:无法解析/tmp/es6"中的./app/index.js"@多 ./app/index.js

  6. 运行 mkdir app &&touch app/index.js(在linux中,创建文件夹'app',然后在app文件夹内创建index.js文件)
  7. 重新运行 npm run build 并成功:

    >es6@1.0.0 构建/tmp/es6>网络包哈希:0578ec98b09c215702c6版本:webpack 3.12.0时间:34ms资产大小块块名称bundle.js 2.59 kB 0 [发出] 主要[0] 多 ./app/index.js 28 字节 {0} [内置][1] ./app/index.js 0 字节 {0} [内置]

How can i fix this error, I'm running Windows 10

When i try to npm run build on the cmd i get this error

C:Usersanai_> npm run build
npm ERR! missing script: build

Here is the log of the run

0 info it worked if it ends with ok
1 verbose cli [ 'C:\Program Files\nodejs\node.exe',
1 verbose cli   'C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'build' ]
2 info using npm@5.5.1
3 info using node@v9.1.0
4 verbose config Skipping project config: C:Usersanai_/.npmrc. (matches userconfig)
5 verbose stack Error: missing script: build
5 verbose stack     at run (C:Program Files
odejs
ode_modules
pmlib
un-script.js:151:19)
5 verbose stack     at C:Program Files
odejs
ode_modules
pmlib
un-script.js:61:5
5 verbose stack     at C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:115:5
5 verbose stack     at C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:402:5
5 verbose stack     at checkBinReferences_ (C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:357:45)
5 verbose stack     at final (C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:400:3)
5 verbose stack     at then (C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:160:5)
5 verbose stack     at ReadFileContext.<anonymous> (C:Program Files
odejs
ode_modules
pm
ode_modules
ead-package-json
ead-json.js:332:20)
5 verbose stack     at ReadFileContext.callback (C:Program Files
odejs
ode_modules
pm
ode_modulesgraceful-fsgraceful-fs.js:78:16)
5 verbose stack     at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:437:13)
6 verbose cwd C:Usersanai_
7 verbose Windows_NT 10.0.15063
8 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "build"
9 verbose node v9.1.0
10 verbose npm  v5.5.1
11 error missing script: build
12 verbose exit [ 1, true ]

Although the error refers to a missing script build, i have placed a 'build' script in my package.json

{
  "name": "es6",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^3.8.1"
  }

}

and this is my webpack.config.js file

module.exports = {
  entry: ['./app/index.js'],
  output: {
    path: './build',
    filename: 'bundle.js'

  }
}

Can anyone tell me how to fix this error or where it is coming from. I am new to using node.js, webpack and the command line. Thanks in advance for your time!

解决方案

  1. Try setting the output.path in webpack.config.js to an absolute path

    module.exports = {
        entry: ['./app/index.js'],
        output: {
            path: __dirname + '/build',
            filename: 'bundle.js'
        }
    }
    

    I don't have Windows (on Linux) but with node 8.16.0 and npm 6.4.1, replicating your package.json and webpack.config.js, I got the following error:

    Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
     - configuration.output.path: The provided value "./build" is not an absolute path!
        -> The output directory as **absolute path** (required).
    

    See more on the absolute-path stuff here

  2. Since you are on Windows, check your file line-endings (CRLF vs LF). If you've cloned code from another place, depending on your git config, it might have carried over different line endings. You can quickly check line endings in the lower-right corner of VSCode.

Steps I did to mimic your setup:

  1. copied your package.json and webpack.config.js
  2. run npm i (or npm install)
  3. run npm run build - received the invalid configuration error noted above
  4. changed the output.path to an absolute path concatenating with __dirname (not sure if Windows is different...)
  5. reran npm run build and got:

    ERROR in multi ./app/index.js
    Module not found: Error: Can't resolve './app/index.js' in '/tmp/es6'
    @ multi ./app/index.js
    

  6. ran mkdir app && touch app/index.js (in linux, creates folder 'app' and then creates the index.js file inside the app folder)
  7. reran npm run build and success:

    > es6@1.0.0 build /tmp/es6
    > webpack
    
    Hash: 0578ec98b09c215702c6
    Version: webpack 3.12.0
    Time: 34ms
        Asset     Size  Chunks             Chunk Names
    bundle.js  2.59 kB       0  [emitted]  main
    [0] multi ./app/index.js 28 bytes {0} [built]
    [1] ./app/index.js 0 bytes {0} [built]
    

这篇关于使用“npm run build"失败并显示“npm ERR!缺少脚本:构建"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆