node local dependency installs as shortcut and nested instead of flat

时间秒杀一切 提交于 2021-01-27 13:06:21

问题


It seems this started when I updated node/npm but I didn't realized until now when I had to delete and re create my node_modules folder.

I have a React Native project which has the core module and one Examples project to showcase the module. The examples project references the module like this in my package.json:

  "dependencies": {
    "module-core": "file:../core"
  },

When I run npm install in the Examples project I was getting this nodule_module structure:

  • node_modules
    • core
    • core_dependency_1
    • core_dependency_2

Now, I get this:

  • node_modules
    • core
      • node_modules
        • core_dependency_1
        • core_dependency_2

At first I thought it had to do with peerDepencies and how npm handled flat/nested dependencies but I have checked and it seems now the core folder is now a shortcut (I am using Windows).

This is breaking my gradle scripts because some are referenced like this:

apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

I could fix this by renaming the links but that would make the build platform/environment dependent.

Also it breaks some other scripts like this one:

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

That is because the project() resolves to my root folder and now I cannot use that either.

I am using npm 5.4.2 and node 8.8.1. Previously I had node 7.4.0.

Is there any flag or way to make npm install the localDependency and not treat it as a shortcut?


回答1:


I finally found the answer. Npm Version 5 indeed changed the way the local dependencies are handled and it just makes npm link, which creates symbolic links or shortcuts in windows.

You can accomplish the same behavior as before with this:

npm install $(npm pack <folder> | tail -1)

Working for me in Windows 10 with git-bash

My final solution was having this package.json in the Example project that used the core:

{
  "name": "core-examples",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "preinstall": "npm pack ../Core | tail -1",
    },
  "dependencies": {
    "core": "file:core-0.0.0.tgz"
  },
  "jest": {
    "preset": "react-native"
  }
}

The preinstall script will generate the tgz file and then you can install as usual. This will avoid having to commit the tgz file to the repository.



来源:https://stackoverflow.com/questions/47420244/node-local-dependency-installs-as-shortcut-and-nested-instead-of-flat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!