0

I have a NodeJS project.

$ npm --version
5.6.0
$ node --version
v8.11.2

I need to replace a text in a file after getting the dependencies, thus I tried using sed in postinstall script. If I don't use -i parameter, it works well. However if I want to change the text permanently in the same file I get an error. My package.json is as follows:

{
  "name": "tmp2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "postinstall": "sed -i 's\/search\/replace\/g' file.txt"
  },
  "author": "",
  "license": "ISC"
}

I get error:

$ npm install

> tmp2@1.0.0 postinstall /Users/mustafa/tmp2
> sed -i 's/search/replace/g' file.txt

sed: 1: "file.txt": invalid command code f
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! tmp2@1.0.0 postinstall: `sed -i 's/search/replace/g' file.txt`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the tmp2@1.0.0 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mustafa/.npm/_logs/2019-01-11T19_04_00_733Z-debug.log

file.txt belongs to my user and its permission is 644.

The same sed command works well if I call it myself on the terminal.

mustafa
  • 3,605
  • 7
  • 34
  • 56

0 Answers0