Npm scripts set `TMPDIR` environment variable #4531

Closed
kumavis opened this Issue Jan 22, 2014 · 19 comments

Comments

Projects
None yet

kumavis commented Jan 22, 2014

I found this bug while using os.tmpdir()

When executing as root on OSX, there is no explicit tmpdir provided from the environment variables. When using os.tmpdir(), node correctly falls back to '/tmp'. However when running scripts via npm, it seems that the environment variable TMPDIR is set to the cwd when undefined. This causes os.tmpdir() to incorrectly return the cwd as the tmp directory.

in my package.json

"scripts": {
    "test": "echo $TMPDIR"
  },
npm test
#=> correct temp dir
sudo npm test
#=> current working directory (??)
node -e "console.log(process.env.TMPDIR)"
#=> correct temp dir
sudo node -e "console.log(process.env.TMPDIR)"
#=> undefined

another test, in my package.json

"scripts": {
    "test": "node -e \"console.log(require('os').tmpdir())\""
  },
npm test
#=> correct temp dir
sudo npm test
#=> current working directory (??)
node -e "console.log(require('os').tmpdir())"
#=> correct temp dir
sudo node -e "console.log(require('os').tmpdir())"
#=> '/tmp'

my system (osx 10.9.1):

node -v
#=> v0.10.22
npm -v
#=> 1.3.14

@iarna iarna added the bug label Oct 3, 2014

CatTail commented Mar 24, 2015

Run into the same issue. It's wired to set current working directory as TMPDIR

kumavis commented Mar 24, 2015

woah blast from the past

I got hit by this bug as well!

Starefossen added a commit to Turistforeningen/Jotunheimr that referenced this issue Apr 15, 2015

nylen commented May 27, 2015

@kumavis I don't believe this is a bug, or it is at least the intended behavior:

kumavis commented May 27, 2015

interesting...
not sure what to recommend here.
this is not really an issue for me, so I don't mind if it gets closed.

nylen commented May 28, 2015

only you can prevent forest fires close out your own issue!

Contributor

othiym23 commented May 28, 2015

Or, you know, I can do it. Closing as behaving as intended. If this is causing problems for your build scripts, @kumavis, you can explicitly set TMPDIR in the environment in which the command is running. Hope this works well enough for you!

@othiym23 othiym23 closed this May 28, 2015

kumavis commented May 28, 2015

forgot to mention: keep being awesome

Contributor

othiym23 commented May 28, 2015

Will do! 👍 Thank you!

export TMPDIR="/tmp"
npm install something

And process.env.TMPDIR it's still ./node_modules/something/...

Inside Docker, maybe this is the issue. But when script is run directly (node install.js), value is as I set it.

Starefossen added a commit to Turistforeningen/node-s3-uploader that referenced this issue Jun 11, 2015

Wow, I'm seeing the same as @erykpiast - it actually blows away my environment's setting to replace the value with the CWD. Crazy.

iameli commented May 6, 2016

Just ran into this. Also in a docker container. Pretty confused.

develar commented May 19, 2016

Pretty confused.

No words. I spend hours to make npm working. A lot of strange bugs. And all closed as intended or as won't fix. No words. But no way to get rid of npm :(

develar commented May 19, 2016

you can explicitly set TMPDIR in the environment in which the command is running.

It doesn't work. Doesn't work. NPM must work as node does, without any magic. Damn.

Contributor

sheerun commented Jun 15, 2016

@othiym23 Please re-open. require('os-tmpdir')() returns current directory instead of /tmp when running with sudo npm test... (sudo clears TMP env variables and npm sets TMPDIR instead).

The scripts runs as intended if you run it with node itself.

Contributor

watilde commented Jun 16, 2016

To not change the value of the environment variable process.TMPDIR and keep the current behavior, we can update the line in lifecycle.js to the following, doesn't it?

--    if (!npm.config.get('unsafe-perm')) env.TMPDIR = null
++    if (!npm.config.get('unsafe-perm')) npm.config.set('tmp', wd)

fson added a commit to fson/babel-loader that referenced this issue Sep 18, 2016

Use standard cache dir as default cacheDirectory
Set the default cache directory as the
[common cache directory](https://github.com/avajs/find-cache-dir),
`./node_modules/.cache/babel-loader`.

Previously, when `cacheDirectory` was set to `true`, babel-loader
tried to use the operating system's temporary directory as a cache
directory.

However, when running npm scripts as the root user,
[npm overrides the TMPDIR environment variable](npm/npm#4531).
This caused the cache files to be created in the project folder itself,
for example when using Docker: facebookincubator/create-react-app#483.

fson added a commit to fson/babel-loader that referenced this issue Sep 18, 2016

Use standard cache dir as default cacheDirectory
Set the default cache directory as the
[common cache directory](https://github.com/avajs/find-cache-dir),
`./node_modules/.cache/babel-loader`.

Previously, when `cacheDirectory` was set to `true`, babel-loader
tried to use the operating system's temporary directory as a cache
directory.

However, when running npm scripts as the root user,
[npm overrides the TMPDIR environment variable](npm/npm#4531).
This caused the cache files to be created in the project folder itself,
for example when using Docker: facebookincubator/create-react-app#483.

@fson fson referenced this issue in babel/babel-loader Sep 18, 2016

Merged

Use standard cache dir as default cacheDirectory #301

4 of 4 tasks complete

fson added a commit to fson/babel-loader that referenced this issue Sep 18, 2016

Use standard cache dir as default cacheDirectory
Set the default cache directory as the
[common cache directory](https://github.com/avajs/find-cache-dir),
`./node_modules/.cache/babel-loader`.

Previously, when `cacheDirectory` was set to `true`, babel-loader
tried to use the operating system's temporary directory as a cache
directory.

However, when running npm scripts as the root user,
[npm overrides the TMPDIR environment variable](npm/npm#4531).
This caused the cache files to be created in the project folder itself,
for example when using Docker: facebookincubator/create-react-app#483.

fson added a commit to fson/babel-loader that referenced this issue Sep 18, 2016

Use standard cache dir as default cacheDirectory
Set the default cache directory as the
[common cache directory](https://github.com/avajs/find-cache-dir),
`./node_modules/.cache/babel-loader`.

Previously, when `cacheDirectory` was set to `true`, babel-loader
tried to use the operating system's temporary directory as a cache
directory.

However, when running npm scripts as the root user,
[npm overrides the TMPDIR environment variable](npm/npm#4531).
This caused the cache files to be created in the project folder itself,
for example when using Docker: facebookincubator/create-react-app#483.

@riggerthegeek riggerthegeek referenced this issue in kmagiera/babel-watch Sep 23, 2016

Closed

System files get generated #24

hzoo added a commit to babel/babel-loader that referenced this issue Oct 26, 2016

Use standard cache dir as default cacheDirectory (#301)
Set the default cache directory as the
[common cache directory](https://github.com/avajs/find-cache-dir),
`./node_modules/.cache/babel-loader`.

Previously, when `cacheDirectory` was set to `true`, babel-loader
tried to use the operating system's temporary directory as a cache
directory.

However, when running npm scripts as the root user,
[npm overrides the TMPDIR environment variable](npm/npm#4531).
This caused the cache files to be created in the project folder itself,
for example when using Docker: facebookincubator/create-react-app#483.

For Docker, we can run with npm start --unsafe-perm

develar commented Jan 13, 2017

@paulodiovani Just set npm config set unsafe-perm true (example: https://github.com/electron-userland/electron-builder/blob/master/docker/7/Dockerfile)

whitecolor commented Jan 13, 2017

npm config set unsafe-perm true this should make os.tmpdir() come true?

Ognian added a commit to Ognian/babel-loader that referenced this issue Feb 27, 2017

Use standard cache dir as default cacheDirectory (#301)
Set the default cache directory as the
[common cache directory](https://github.com/avajs/find-cache-dir),
`./node_modules/.cache/babel-loader`.

Previously, when `cacheDirectory` was set to `true`, babel-loader
tried to use the operating system's temporary directory as a cache
directory.

However, when running npm scripts as the root user,
[npm overrides the TMPDIR environment variable](npm/npm#4531).
This caused the cache files to be created in the project folder itself,
for example when using Docker: facebookincubator/create-react-app#483.

mykmelez added a commit to mykmelez/qbrt that referenced this issue May 19, 2017

run tests as non-root user
npm sets TMPDIR to the cwd if running as the root user, per npm/npm#4531, which is the, ahem, root cause of the test failure in run-path-missage-package.js.  There are two workarounds: invoke npm with --unsafe-perm or run it as a different user.  This commit attempts to do the latter.

mykmelez added a commit to mykmelez/qbrt that referenced this issue May 19, 2017

run tests as non-root user
npm sets TMPDIR to the cwd if running as the root user, per npm/npm#4531, which is the, ahem, root cause of the test failure in run-path-missage-package.js.  There are two workarounds: invoke npm with --unsafe-perm or run it as a different user.  This commit attempts to do the latter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment