Skip to content

Unexpected token '.' when running npm install with node 17.4.0 and explicit npm installation #310

Closed

Description

@Maxim-Mazurok

On windows 11 I can't use npm with node 17.4.0 when I explicitly ask to install npm 8.3.1 (which is shipped with this node version by default):

C:\Users\maxim\my-project>python -m nodeenv nodeenv --node=17.4.0 --npm=8.3.1 --with-npm
 * Install prebuilt node (17.4.0) ..... done.
symbolic link created for nodeenv\Scripts\nodejs.exe <<===>> node.exe
 * Install npm.js (8.3.1) ...
C:\Users\maxim\my-project>nodeenv\Scripts\activate.bat   
(nodeenv) C:\Users\maxim\my-project>node -v
v17.4.0

(nodeenv) C:\Users\maxim\my-project>npm -v
8.3.1

(nodeenv) C:\Users\maxim\my-project>npm install something
npm ERR! Unexpected token '.'

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\maxim\AppData\Local\npm-cache\_logs\2022-06-23T12_13_38_115Z-debug-0.log

(nodeenv) C:\Users\maxim\my-project>

Here's a complete log:

0 verbose cli [
0 verbose cli   'C:\\Users\\maxim\\my-project\\nodeenv\\Scripts\\node.exe',
0 verbose cli   'C:\\Users\\maxim\\my-project\\nodeenv\\Scripts\\node_modules\\npm\\bin\\npm-cli.js',
0 verbose cli   'install',
0 verbose cli   'something'
0 verbose cli ]
1 info using [email protected]
2 info using [email protected]
3 timing npm:load:whichnode Completed in 0ms
4 timing config:load:defaults Completed in 1ms
5 timing config:load:file:C:\Users\maxim\my-project\nodeenv\Scripts\node_modules\npm\npmrc Completed in 1ms
6 timing config:load:builtin Completed in 1ms
7 timing config:load:cli Completed in 1ms
8 timing config:load:env Completed in 1ms
9 timing config:load:file:C:\Users\maxim\my-project\.npmrc Completed in 2ms
10 timing config:load:project Completed in 3ms
11 timing config:load:file:C:\Users\maxim\.npmrc Completed in 1ms
12 timing config:load:user Completed in 1ms
13 timing config:load:file:C:\Users\maxim\my-project\nodeenv\Scripts\etc\npmrc Completed in 0ms
14 timing config:load:global Completed in 0ms
15 timing config:load:validate Completed in 1ms
16 timing config:load:credentials Completed in 1ms
17 timing config:load:setEnvs Completed in 1ms
18 timing config:load Completed in 12ms
19 timing npm:load:configload Completed in 12ms
20 timing npm:load:setTitle Completed in 0ms
21 timing config:load:flatten Completed in 2ms
22 timing npm:load:display Completed in 3ms
23 verbose logfile C:\Users\maxim\AppData\Local\npm-cache\_logs\2022-06-23T12_13_38_115Z-debug-0.log
24 timing npm:load:logFile Completed in 6ms
25 timing npm:load:timers Completed in 0ms
26 timing npm:load:configScope Completed in 0ms
27 timing npm:load Completed in 23ms
28 verbose stack C:\Users\maxim\my-project\nodeenv\Scripts\node_modules\npm\node_modules\libnpmfund:1
28 verbose stack ../workspaces/libnpmfund
28 verbose stack ^
28 verbose stack
28 verbose stack SyntaxError: Unexpected token '.'
28 verbose stack     at Object.compileFunction (node:vm:352:18)
28 verbose stack     at wrapSafe (node:internal/modules/cjs/loader:1026:15)
28 verbose stack     at Module._compile (node:internal/modules/cjs/loader:1061:27)
28 verbose stack     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
28 verbose stack     at Module.load (node:internal/modules/cjs/loader:975:32)
28 verbose stack     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
28 verbose stack     at Module.require (node:internal/modules/cjs/loader:999:19)
28 verbose stack     at require (node:internal/modules/cjs/helpers:102:18)
28 verbose stack     at Object.<anonymous> (C:\Users\maxim\my-project\nodeenv\Scripts\node_modules\npm\lib\utils\reify-output.js:16:38)
28 verbose stack     at Module._compile (node:internal/modules/cjs/loader:1097:14)
29 verbose cwd C:\Users\maxim\my-project
30 verbose Windows_NT 10.0.22000
31 verbose argv "C:\\Users\\maxim\\my-project\\nodeenv\\Scripts\\node.exe" "C:\\Users\\maxim\\my-project\\nodeenv\\Scripts\\node_modules\\npm\\bin\\npm-cli.js" "install" "something"
32 verbose node v17.4.0
33 verbose npm  v8.3.1
34 error Unexpected token '.'
35 verbose exit 1
36 timing npm Completed in 267ms
37 verbose code 1
38 error A complete log of this run can be found in:
38 error     C:\Users\maxim\AppData\Local\npm-cache\_logs\2022-06-23T12_13_38_115Z-debug-0.log

Here's a very similar issue with nvm4w: nvm-windows/nvm#814

This works:

python -m nodeenv nodeenv --node=17.4.0
npm install -g [email protected]
npm install something

Activity

  1. Maxim-Mazurok commented on Jun 24, 2022

    @Maxim-Mazurok
    ContributorAuthor

    Another example: node 18.3.0 and npm 8.3.1.

    Doesn't work when installed using nodeenv, but if I install node using nodeend, activate env, then run npm i -g [email protected] - it works fine.

  2. vitorgamer58 commented on Aug 20, 2022

    @vitorgamer58

    The same here with node 16.14.2

    I've changed my node version with nvm to 16.12.0 and the problem was fixed.

  3. cjablonski76 commented on Mar 7, 2023

    @cjablonski76

    Posted what fixed this for me here:
    nvm-windows/nvm#814 (comment)

  4. ekalinin commented on Sep 19, 2026

    @ekalinin
    Owner

    I think I can pin down the root cause here, and it is not node or npm version specific.

    install_npm_win downloads the source repository of npm, not the published npm package:

    npm_url = 'https://github.com/npm/cli/archive/v%s.zip' % args.npm
    ...
    with zipfile.ZipFile(npm_contents, 'r') as zipf:
        zipf.extractall(src_dir)
    npm_ver = 'cli-%s' % args.npm
    shutil.copytree(join(src_dir, npm_ver), node_modules_path)

    In that repo the npm workspaces are linked into node_modules by symlink. A GitHub zip archive stores a symlink as a regular file whose contents are the link target, and Python's zipfile.extractall does not restore it as a link. cli-8.3.1.zip has 12 such entries, all under node_modules/, including exactly the one from your stack trace:

    cli-8.3.1/node_modules/@npmcli/arborist -> ../../workspaces/arborist
    cli-8.3.1/node_modules/libnpmaccess     -> ../workspaces/libnpmaccess
    cli-8.3.1/node_modules/libnpmdiff       -> ../workspaces/libnpmdiff
    cli-8.3.1/node_modules/libnpmexec       -> ../workspaces/libnpmexec
    cli-8.3.1/node_modules/libnpmfund       -> ../workspaces/libnpmfund
    cli-8.3.1/node_modules/libnpmhook       -> ../workspaces/libnpmhook
    ...
    

    So after extraction node_modules/libnpmfund is a one line text file containing ../workspaces/libnpmfund, and require()-ing it fails on the first character of that text. That is precisely what the log says:

    28 verbose stack C:\...\node_modules\npm\node_modules\libnpmfund:1
    28 verbose stack ../workspaces/libnpmfund
    28 verbose stack ^
    28 verbose stack SyntaxError: Unexpected token '.'
    

    It also explains why npm i -g [email protected] inside the env works: that installs the published package, which has no symlinks.

    Suggested fix

    Fetch the registry tarball instead of the GitHub source archive. Numbers I measured today:

    source entries symlinks longest internal path
    github.com/npm/cli/archive/v8.3.1.zip 9673 12 196
    registry.npmjs.org/npm/-/npm-8.3.1.tgz 1894 0 76
    github.com/npm/cli/archive/v11.6.2.zip 10051 0 196
    registry.npmjs.org/npm/-/npm-11.6.2.tgz 2133 0 89

    The tarball already ships everything install_npm_win copies out: package/bin/npm, package/bin/npm.cmd, package/bin/npm-cli.js (and npx, npx.cmd).

    Three more things it fixes along the way:

    1. --with-npm with the default --npm=latest currently 404s, because the URL becomes .../archive/vlatest.zip. The registry resolves it directly: registry.npmjs.org/npm/latest gives dist.tarball.
    2. It removes the MAX_PATH exposure that was the remaining part of Windows installing npm: shutil.Error / WindowsError [Error 206] The filename or extension is too long #190. 196 chars of archive depth leaves only about 37 chars for the env directory once copied into Scripts\node_modules\npm; 89 chars leaves about 144. Most of that depth comes from workspaces/arborist/test/fixtures/..., test fixtures that have no business being in an installed npm anyway.
    3. The extra raw.githubusercontent.com download of bin/npm in the Cygwin branch becomes unnecessary, since package/bin/npm is in the tarball.

    The symlink hazard is not hypothetical for current versions either: zipfile.extractall will keep flattening any symlink npm reintroduces, whereas the published tarball has none by construction.

  5. added a commit that references this issue on Sep 28, 2026
    8b67db3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions