This repository has been archived by the owner. It is now read-only.

Npm scripts set `TMPDIR` environment variable #4531

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

Comments

Projects
None yet
@kumavis

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

This comment has been minimized.

CatTail commented Mar 24, 2015

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

@kumavis

This comment has been minimized.

kumavis commented Mar 24, 2015

woah blast from the past

@Starefossen

This comment has been minimized.

Starefossen commented Apr 15, 2015

I got hit by this bug as well!

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

@nylen

This comment has been minimized.

nylen commented May 27, 2015

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

@kumavis

This comment has been minimized.

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

This comment has been minimized.

nylen commented May 28, 2015

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

@othiym23

This comment has been minimized.

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

This comment has been minimized.

kumavis commented May 28, 2015

forgot to mention: keep being awesome

@othiym23

This comment has been minimized.

Contributor

othiym23 commented May 28, 2015

Will do! 👍 Thank you!

@erykpiast

This comment has been minimized.

erykpiast commented Jun 3, 2015

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

@coffenbacher

This comment has been minimized.

coffenbacher commented Jan 5, 2016

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

This comment has been minimized.

iameli commented May 6, 2016

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

@develar

This comment has been minimized.

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

This comment has been minimized.

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.

@sheerun

This comment has been minimized.

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.

@watilde

This comment has been minimized.

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)

@aaronjensen aaronjensen referenced this issue Jul 8, 2016

Closed

Npm overrides TMPDIR for root users #13325

2 of 13 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: facebook/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: facebook/create-react-app#483.

@fson fson referenced this issue 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: facebook/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: facebook/create-react-app#483.

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: facebook/create-react-app#483.
@paulodiovani

This comment has been minimized.

paulodiovani commented Jan 13, 2017

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

@develar

This comment has been minimized.

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

This comment has been minimized.

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 (babel#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: facebook/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 subscribe to this conversation on GitHub. Already have an account? Sign in.