Using "npm run build" fails with "npm ERR! missing script: build"

NpmWebpackAtom Editor

Npm Problem Overview


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:\Users\anai_> 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:\Users\anai_/.npmrc. (matches userconfig)
5 verbose stack Error: missing script: build
5 verbose stack     at run (C:\Program Files\nodejs\node_modules\npm\lib\run-script.js:151:19)
5 verbose stack     at C:\Program Files\nodejs\node_modules\npm\lib\run-script.js:61:5
5 verbose stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:115:5
5 verbose stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:402:5
5 verbose stack     at checkBinReferences_ (C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:357:45)
5 verbose stack     at final (C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:400:3)
5 verbose stack     at then (C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:160:5)
5 verbose stack     at ReadFileContext.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:332:20)
5 verbose stack     at ReadFileContext.callback (C:\Program Files\nodejs\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:78:16)
5 verbose stack     at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:437:13)
6 verbose cwd C:\Users\anai_
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!

Npm Solutions


Solution 1 - Npm

  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. Visual Studio Code File Indicators

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]
    

Solution 2 - Npm

First check you have .babelrc file in your root folder.

if you already have

Add this in your package.json file

"scripts": {
    "dev": "webpack --config=Scripts/config/webpack.dev.js --watch",
    "build": "webpack --config=Scripts/config/webpack.prod.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

Solution 3 - Npm

You put "npm run build && gh-pages -d build" in your deploy script, but you need to tell npm what to do when npm run build is being run. Configure build to whatever command needs to run to build your project.

Since you are using webpack, I assume it would be webpack --config webpack.conf.js. With gulp, gulp build, with TypeScript tsc -p tsproject.json, and so on. It depends on your project.

you can check out the full writeup here: https://github.com/npm/npm/wiki/Troubleshooting#a-brief-note-on-the-built-in-windows-configuration

Solution 4 - Npm

Also it might be helpful to delete empty ui src folder (or where you usually store package-lock.json and etc). This might appear for example when you checkout and switching between different branches and some branch has already deleted ui src folder, but git doesn't delete empty folders.

Solution 5 - Npm

including "build": "webpack --config webpack.conf.js" didn't work!

I tried webpack --config webpack.config.js which was clean run but after upgrading to 4.41.2 and a little while I used,

"scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
      "watch": "webpack --watch",
      "build": "webpack"
    },

which worked well for me. It's good to make sure build is correct for the version since I'm using webpack 4.41.2

Solution 6 - Npm

check whether in your package.json build is there or not , if not then mention it inside scripts

 "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch": "ts-node-dev --respawn src/Union.ts",
    "serve": "ts-node-dev src/function.ts",
    "build": "tsc"
  },

Solution 7 - Npm

I had the same issue, but then I realized that I forget to install the Angular CLI,

So what I did to fix the problem:

  1. I execute this command into my npm installation folder :

npm install -g @angular/cli

  1. Then I create a new Angular project, ng new project-name ,

After that I was able to execute the npm run build,

Hope this help.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnaizingView Question on Stackoverflow
Solution 1 - NpmtvealView Answer on Stackoverflow
Solution 2 - NpmNawinView Answer on Stackoverflow
Solution 3 - NpmJoseph CharlesView Answer on Stackoverflow
Solution 4 - NpmMirzaView Answer on Stackoverflow
Solution 5 - NpmArun PanneerselvamView Answer on Stackoverflow
Solution 6 - NpmYeshiView Answer on Stackoverflow
Solution 7 - NpmYacinoView Answer on Stackoverflow